By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,982 Members | 1,939 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,982 IT Pros & Developers. It's quick & easy.

Write during reading a file

P: n/a
I must read a txt file and when I found a line beginning with the word
"Backup", I must replace this line with "Save"

The read and the write must done into the same file!

I know how to read line after line a file (I use StreamReader), but how
can I write during I read the file ??

--
Ceci est une signature automatique de MesNews.
Site : http://www.mesnews.net

Nov 21 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a

"bnob" <bn**@bluewin.ch> wrote in message
news:mn***********************@bluewin.ch...
I must read a txt file and when I found a line beginning with the word
"Backup", I must replace this line with "Save"

The read and the write must done into the same file!

I know how to read line after line a file (I use StreamReader), but how
can I write during I read the file ??

--
Ceci est une signature automatique de MesNews.
Site : http://www.mesnews.net

Check out the FileStream class Seek() method in the help.
<quote>
FileStream objects support random access to files using the Seek method.
Seek allows the read/write position to be moved to any position within the
file. This is done with byte offset reference point parameters. The byte
offset is relative to the seek reference point, which can be the beginning,
the current position, or the end of the underlying file, as represented by
the three properties of the SeekOrigin class.

Note Disk files always support random access. At the time of
construction, the CanSeek property value is set to true or false depending
on the underlying file type. Specifically, if the underlying file type is
FILE_TYPE_DISK, as defined in winbase.h, the CanSeek property value is true.
Otherwise, the CanSeek property value is false.
</quote>
--
Peter [MVP Visual Developer]
Jack of all trades, master of none.
Nov 21 '05 #2

P: n/a
Bnob,

When it is a normal text file than the answer is simple.
The read and the write must done into the same file!
You cannot, you have to make a copy with another name, read that, write a
new one and when succesfull ready the new file, delete the old (when you
wish that).

There is a "temp" method to get a nice name.

http://msdn.microsoft.com/library/de...enametopic.asp

I hope this helps?

Cor

"bnob" <bn**@bluewin.ch>
"Backup", I must replace this line with "Save"

The read and the write must done into the same file!

I know how to read line after line a file (I use StreamReader), but how
can I write during I read the file ??

Nov 21 '05 #3

P: n/a
Peter,

I had read that something so I knew there was something with my answer.
http://msdn.microsoft.com/library/de...mberstopic.asp

However how can this be practical done.

In my opinion than it has to be something as.
You find a word "Backup", write on that start place Save and have to move
all postitions starting after the Save with -2 (which theoretical goes the
other way would be even a bigger problem) and than set in the two last byes
something as 00. And than do this procedure again until you find nothing.

Or have you a more efficient method?

Cor
"Peter van der Goes" <p_**********@toadstool.u>
bericht news:OJ*************@TK2MSFTNGP12.phx.gbl...

"bnob" <bn**@bluewin.ch> wrote in message
news:mn***********************@bluewin.ch...
I must read a txt file and when I found a line beginning with the word
"Backup", I must replace this line with "Save"

The read and the write must done into the same file!

I know how to read line after line a file (I use StreamReader), but how
can I write during I read the file ??

--
Ceci est une signature automatique de MesNews.
Site : http://www.mesnews.net

Check out the FileStream class Seek() method in the help.
<quote>
FileStream objects support random access to files using the Seek method.
Seek allows the read/write position to be moved to any position within the
file. This is done with byte offset reference point parameters. The byte
offset is relative to the seek reference point, which can be the
beginning,
the current position, or the end of the underlying file, as represented by
the three properties of the SeekOrigin class.

Note Disk files always support random access. At the time of
construction, the CanSeek property value is set to true or false depending
on the underlying file type. Specifically, if the underlying file type is
FILE_TYPE_DISK, as defined in winbase.h, the CanSeek property value is
true.
Otherwise, the CanSeek property value is false.
</quote>
--
Peter [MVP Visual Developer]
Jack of all trades, master of none.

Nov 21 '05 #4

P: n/a

"Cor Ligthert" <no************@planet.nl> wrote in message
news:OS**************@TK2MSFTNGP11.phx.gbl...
Peter,

I had read that something so I knew there was something with my answer.
http://msdn.microsoft.com/library/de...mberstopic.asp
However how can this be practical done.

In my opinion than it has to be something as.
You find a word "Backup", write on that start place Save and have to move
all postitions starting after the Save with -2 (which theoretical goes the
other way would be even a bigger problem) and than set in the two last byes something as 00. And than do this procedure again until you find nothing.

Or have you a more efficient method?

Cor

The suggestion was in consideration of the OP's stated requirement:
<quote>
The read and the write must done into the same file!
</quote>
If you choose to interpret that as allowing for creation of a second file,
based on the "old master", then the simple expedient of reading the "old
master" sequentially, parsing each input record for "Backup", and replacing
each qualifying record with a record containing "Save" would be easier to
implement.
So, if the OP didn't mean "same file" literally...

--
Peter [MVP Visual Developer]
Jack of all trades, master of none.
Nov 21 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.