Write to an existing ASCII file

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

My experience with reading/writing files is to open one file, read it into
memory close it and open another file and write to it.

Now I would like to learn how to read and write to the same file. My
problem is that I need to open a file, read the first line, change the line
somewhat and write the line back to the file (overwriting the original line).

Can anyone help?

Thanks,
Mark
 
Mark said:
My experience with reading/writing files is to open one file, read it into
memory close it and open another file and write to it.

Now I would like to learn how to read and write to the same file. My
problem is that I need to open a file, read the first line, change the
line
somewhat and write the line back to the file (overwriting the original
line).

Check out 'System.IO.StreamWriter', 'System.IO.StreamReader' (samples in the
documentation) and 'System.Text.Encoding.Ascii'. Note that stream reader
and writer accept an encoding in their constructors.
 
Hi Mark,

Open the File as a FileStream object using the New
FileStream("FileName", FileMode, FileAccess) constructor and set the
FileAccess to 'ReadWrite'. Then, use a StreamReader to read, and a
StreamWriter to write data, as reqd.

Regards,

Cerebrus.
 
Now I would like to learn how to read and write to the same file. My
problem is that I need to open a file, read the first line, change the
line
somewhat and write the line back to the file (overwriting the original
line).

If the new line is longer or shorter than the original line, what should
happen?
 
Back
Top