Home All Groups Group Topic Archive Search About

Write to an existing ASCII file

Author
10 Mar 2006 8:51 PM
Mark
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

Author
11 Mar 2006 1:27 AM
Herfried K. Wagner [MVP]
"Mark" <M***@discussions.microsoft.com> schrieb:
> 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.

--
M S   Herfried K. Wagner
M V P  <URL:http://dotnet.mvps.org/>
V B   <URL:http://classicvb.org/petition/>
Author
11 Mar 2006 6:34 AM
Cerebrus
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.
Author
12 Mar 2006 1:26 AM
Homer J Simpson
"Mark" <M***@discussions.microsoft.com> wrote in message
news:9BE1059E-45B0-4956-BA9C-38776DA250F1@microsoft.com...

> 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?