PDA

Click to See Complete Forum and Search --> : Help! File I/O problem


netSurfer
Jan 24th, 2000, 10:41 PM
I need to open a file and both read and write to it. It's a CSV file that I need to look through to see if a line matches the one I need to add and then write the changes to that line. I can open a file to read or to write but I don't know how to do both.

Rick H
Jan 24th, 2000, 10:49 PM
Use this syntax

Open "myfile.csv" For Append Access Read Write As #1

Hope this helps

netSurfer
Jan 25th, 2000, 11:18 AM
Rick, It will let me open it but it goes to end of file. I need to search through the file before I write to it.

I use:

Do Until EOF(intFreeFile)
Line Input #intFreeFile, strLine
If Mid(strLine, 1, InStr(1, strLine, ",") - 1) = intRecordID Then
Exit Do
End If
Loop

I need to search through the file to see if there's a record already exisiting. Using the append doesn't allow me to with this code because the EOF is always TRUE. Any other ideas or is there different code I can use for this?

Rick H
Jan 25th, 2000, 11:31 AM
I see the problem, why dont you create a copy of the file (line by line) as you are searching, apply the edits (or add ins) to the new copy. Therefore you can open the old one as INPUT and the new one as OUTPUT.

Bit messy but will do the job.

[This message has been edited by Rick H (edited 01-25-2000).]

netSurfer
Jan 25th, 2000, 11:36 AM
Yuck. :-( I've been thinking about that too. It would work, but I was really hoping for another, smoother and less resource-dependable method. I'm afraid I'll probably have to do it though :-(. Thanks for the help.