Results 1 to 15 of 15

Thread: [RESOLVED] how to move between lines when writing to a text file

  1. #1
    Member
    Join Date
    Apr 09
    Posts
    39

    Resolved [RESOLVED] how to move between lines when writing to a text file

    i am using the following code to write to a text file

    Code:
    dim A1 as string
    
    A1= "......."
    
    On Error GoTo l1
    Open App.Path & "\Textfile.txt" For Append As #1
    l1:
    On Error GoTo l2
    Print #1, A1
    Close #1
    l2:
    which go down to a new line each time its used, however at some points i need to go back and complete to the first line an additional string and then go back down.
    how can i move between lines, and to the specific point?

  2. #2
    Hyperactive Member
    Join Date
    Oct 06
    Posts
    388

    Re: how to move between lines when writing to a text file

    I believe that you can not achieve what you wish using the simple code you quote. The OPEN statement allows three options - For Input, For Output and For Append. Only the last of these - APPEND - allows the addition of text and as the name implies it can only be added at the end.

    I have solved this problem by the use of a RichTextBox. Place a large RTBox on your form and then import your txt file into it. You can then read it all and edit it much as when using MS Word or Notepad or Wordpad in word-processing. Thus you can add text at the bottom, in the middle and at the beginning. Also you can change any of the text already written. When you have completed what you want to do with your text, you then save it back into your Textfile.txt path and that will then contain the version as you have just modified it.

    I find the RTBox a powerful and very useful control, but one which seldom gets much (or any) mention in a lot of VB6 books.

    If you need help with the code to achieve the above with a RTBox, make another post to this thread and I will generate an example.

    camoore

    Wales, UK

  3. #3
    PowerPoster
    Join Date
    Feb 12
    Location
    West Virginia
    Posts
    4,978

    Re: how to move between lines when writing to a text file

    Text files are written top to bottom. If you need to change something in a text file then you read the file and write it again adding what you need to at the proper time during the write or to the string before it is written.

  4. #4
    Hyperactive Member
    Join Date
    Oct 06
    Posts
    388

    Re: how to move between lines when writing to a text file

    Datamiser has repeated the concept of my advice. Using simple code you can only append new text at the end of a .txt file. To do otherwise requires the file to be "read" into some sort of string variable (I suggested the RichTexBox) manipluated and then saved (re-read) back as the original file.txt path.

    Manipulation can be carried out under VB6 if you subsequently copy the RTBox file into a String variable and then use String Manipulation to change this automatically.

    In order to do this you would need a very rigidly defined format for your source .txt file - in order that String Manipulation could accurately and unambiguously determine which element of the String (=.txt file) you want to change or add to.

    camoore

    Wales, UK

  5. #5
    Frenzied Member
    Join Date
    Aug 11
    Location
    B.C., Canada
    Posts
    1,946

    Re: how to move between lines when writing to a text file

    If you know the line # to modify then it easy you can read entire file line by line and add a counter when your counter hits your line number you append to file

    Code:
    Private Sub Command1_Click()
    Dim sLineText As String
    Dim sTemp as String
    Dim ff As Integer
    Dim iLineCount As Integer
    
    
    
    Open "C:\textfile.txt" For Input As #ff
       Do While Not EOF(ff)
          Line Input #ff, sLineText
          sTemp = sTemp & sLineText & vbNewLine
          iLineCount = iLineCount + 1
    If iLineCount = 4 Then
    sTemp = sTemp & "My string to add." 'here i append to the file on line 4
    End If
       Loop
    Close #ff
    
    sTemp = Left(sTemp,Len(sTemp) - 1) 'Remove last vbNewLine
    
    Open "C:\textfile.txt" For Output As #ff
    Print #ff, sTemp
    Close #ff
    End Sub
    This is one of the ways if you know where to append if not you will need to search for string and then append after the string

  6. #6
    PowerPoster
    Join Date
    Feb 12
    Location
    West Virginia
    Posts
    4,978

    Re: how to move between lines when writing to a text file

    Yes and there are other ways as well.

    If your file has fixed lenght lines then it is easy to modify an existing line so long as you know the line number, not that it is hard using the ways given just requires a few more steps. If a file has fixed lenght records then you can use Random Access and read or write to any line in the file without the need to create a new file or read anything other than the line in question.

  7. #7
    Member
    Join Date
    Apr 09
    Posts
    39

    Re: how to move between lines when writing to a text file

    you can then read it all and edit it much as when using MS Word or Notepad
    but i dont want to make these changes manually. because i cant. it's a numerous number of equations with varying variables and coefficients according to ather factors.

    so i guess ill ask the question again, how to move between lines in richtextbok and how to add texts at specific places programatically
    Last edited by amd711; Aug 4th, 2012 at 04:45 PM.

  8. #8
    Hyperactive Member
    Join Date
    Oct 06
    Posts
    388

    Re: how to move between lines when writing to a text file

    amd711. Then you have not adequately described what you want to do.

    There are many ways of using VB6 to modify a .txt document, and the best method to adopt depends upon just what you want to achieve.

    Please define your requirement more exactly. Programming is an exact "science" but it can only be accomplished exactly given an exact definition of the objective.

    All of the replies to your post relate to string management, but strings can only be "managed" if you define EXACTLY how you want to manage them.

    camoore

    Wales, UK

  9. #9
    PowerPoster
    Join Date
    Feb 12
    Location
    West Virginia
    Posts
    4,978

    Re: how to move between lines when writing to a text file

    Quote Originally Posted by amd711 View Post
    but i dont want to make these changes manually. because i cant. it's a numerous number of equations with varying variables and coefficients according to ather factors.

    so i guess ill ask the question again, how to move between lines in richtextbok and how to add texts at specific places programatically
    You would read the file line by line using the line input # method. You would write these lines out to a new file as you go. When you get to a line you want to modify you make your change to that line which would be in the form of a string variable in memory then you write the modified line to the new file instead of the original line then continue on until you have did all the lines in the file. Nothing complicated about it you just have to read and write top to bottom making changes at the proper time. Once your line is written to the file you can not go back and edit that line wihtout once again reading the file and writing the changed file again.

    Code:
    Dim InFileHandle as Integer
    Dim OutFileHandle as Integer
    
    Dim TheLine as String
    Dim LineNumber As Integer
    
    InFileHandle=FreeFile
    Open InputFileName For Input As #InFileHandle
    OutFileHandle=FreeFile
    Open OutputFileName for Output as #OutFileHandle
    
    Do While Not EOF(InFileHandle)
        LineNumber=LineNumber+1
        Line Input #InFileHandle,TheLine
        if LineNumber=YourTargetLineNumber
             'Make changes to TheLine here
        End If
        Print #OutFileHandle, TheLine
    Loop
    Close #OutFileHandle
    Close #InFileHandle
    Last edited by DataMiser; Aug 4th, 2012 at 09:33 PM.

  10. #10
    PowerPoster
    Join Date
    Jul 06
    Location
    Maldon, Essex. UK
    Posts
    5,387

    Re: how to move between lines when writing to a text file

    As cadmoor has pointed out, there are numerous methods to edit data. From what you've told us so far it's just about impossible to recommend the 'best' solution. The fact that you're not using something like Notepad implies that it's not just a simple matter of going through lines of the file making changes manually.

    So, how will you identify candidates for change?
    Will there be 'global' changes (eg change all X ^2 to X ^ 3 in all records)?
    Do you need to see the data before and after changes or should it all be done 'behind the scenes'?
    Do you need to save the original file or make changes 'in situe', thus losing the original data?

    If you can answer those questions it'll give people a better idea of how to guide you.

  11. #11
    Member
    Join Date
    Apr 09
    Posts
    39

    Re: how to move between lines when writing to a text file

    how will you identify candidates for change?
    changes will be at the first line only, and I won't change it completely i will continously add to it

    Do you need to see the data before and after changes or should it all be done 'behind the scenes'?
    no i dont

    Will there be 'global' changes (eg change all X ^2 to X ^ 3 in all records)?
    like i said, the first line only, an addition will be added to it each time an equation is built below.

    Do you need to save the original file or make changes 'in situe', thus losing the original data?
    there won't be an original file every thing will be built from scratch.
    Last edited by amd711; Aug 6th, 2012 at 08:29 PM.

  12. #12
    PowerPoster
    Join Date
    Jul 06
    Location
    Maldon, Essex. UK
    Posts
    5,387

    Re: how to move between lines when writing to a text file

    Ok, if I understand you correctly you are going to add records to a file a line at a time. Once a line has been added to the file you will not want to change that line. If that's correct then it's a reasonably simple matter to collect the input (ie the data you're going to add), open the file for Append and Print the data to the file, repeat until all the data has been Printed to the file, then close the file. If the input is coming from the user (e.g. they're typing it into a TextBox then you'll need some way of determining when they've finished. In its simplest form it might look something like this:
    Code:
    Option Explicit
    Private intFile As Integer
    
    Private Sub cmdAdd_Click()
    '
    '
    ' When the user clicks on cmdAdd it will output
    ' the contents of the textbox to the file
    ' unless it was a blank line in which case
    ' the file is closed
    ' and the program exits
    '
    If txtEquation.Text <> "" Then
        Print #intFile, txtEquation.Text
        txtEquation.Text = ""
    Else
        Close intFile
        MsgBox "Additions to the file are complete"
        Unload Me
    End If
    End Sub
    
    Private Sub Form_Load()
    '
    ' At start up clear the textbox and open the file
    '
    txtEquation.Text = ""
    intFile = FreeFile()
    Open "C:\MyData\Equations.text" For Append As intFile
    End Sub
    Last edited by Doogle; Aug 7th, 2012 at 12:52 AM. Reason: Spelling

  13. #13
    Hyperactive Member
    Join Date
    Oct 06
    Posts
    388

    Re: how to move between lines when writing to a text file

    I still fail to understand fully what the OP wants. He has stated that he wants "continuously to add to the first line only" yet has also stated that "there will not be an original file. Everything will be built from scratch".

    If there is no original file, then what first line is to be added to?

    If the whole file is being generated from code each time "something" takes place, then the code to generate the first line must be in the program and he simply needs to alter what that code writes as the first line.

    If after starting to compile the file by code the first line needs to be changed/added to, then why not simply start from scratch again?

    Even this could be avoided. Have 2 text boxes, one for the first line, the other for the rest of the file. If it becomes necessary as a result of subsequent action(s) to alter the first line, then just amend the first text box. Then when all action(s) are complete, join the string of the second text box to that of the first and save the lot as a new .txt.

    Is that not more simple?

    camoore

    Wales, UK

  14. #14
    Member
    Join Date
    Apr 09
    Posts
    39

    Re: how to move between lines when writing to a text file

    big thanks to you all
    especially camoore, Thanks alot

  15. #15
    Hyperactive Member
    Join Date
    Oct 06
    Posts
    388

    Re: how to move between lines when writing to a text file

    Pleased that we were able to be of assistance. If your problem is now solved, please mark the thread as RESOLVED (see thread tools at the top).

    camoore

    Wales, UK

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •