Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

read/write text

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
how do i read and write from a certain line of a text file. for example: i want to read from line 1 and line 3 of a text file (line one will be opened in lin1.text and line 3 will be opened from line3.text). after i have edited those lines, i want to write them back to line 1 and line 3. im not that versed in vb and text files, so can anyone out there help me out with my problem?
 
frankmurry,

For a sequential text file, here's the examples for writing and reading.
Code:
   Dim MyString, MyNumber
   Open "TESTFILE" For Input As #1   ' Open file for input.
   Do While Not EOF(1)   ' Loop until end of file.
      Input #1, MyString, MyNumber   ' Read data into two variables.
      Debug.Print MyString, MyNumber   ' Print data to the Immediate window.
   Loop
   Close #1   ' Close file.


   Open "TESTFILE" For Output As #1   ' Open file for output.
   Write #1, "Hello World", 234   ' Write comma-delimited data.
   Write #1,   ' Write blank line.

   Dim MyBool, MyDate, MyNull, MyError
   ' Assign Boolean, Date, Null, and Error values.
   MyBool = False : MyDate = #February 12, 1969# : MyNull = Null
   MyError = CVErr(32767)
   ' Boolean data is written as #TRUE# or #FALSE#. Date literals are 
   ' written in universal date format, for example, #1994-07-13# 
   'represents July 13, 1994. Null data is written as #NULL#. 
   ' Error data is written as #ERROR errorcode#.
   Write #1, MyBool ; " is a Boolean value"
   Write #1, MyDate ; " is a date"
   Write #1, MyNull ; " is a null value"
   Write #1, MyError ; " is an error value"
   Close #1   ' Close file.

[code]

I'm not sure how to write back in a certain location in the file.  I looked at some of our code and since our file is small, we create it completely over into a variable and then write it to the file.  I do not know if this would work for you.  There should be a way to write to a specific location within a file.  

HTH,
Patty
 
VB does not handle reading from and writing to specific places in a file that well.
Normally, the posts that will follow (as they have in many previous post about much the same thing, will suggest:
1) read all the file line by line and edit the line you are interested in, writing out each line in turn as you go to another file, thus creating a copy of the original file with the editted text.
2) Use an .ini file, where you can specify the exact part of the file you want to read / write, bu then the file wil look like this:
[Section Heading]
Line1=xxxxxxxxxx
Line2=yyyyyyyyy
Line3=zzzzzzzzzzzz
.......
instead of just your simple file:
xxxxxxxxxx
yyyyyyyyy
zzzzzzzzzzzz

Hope this helps

Simon
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top