Write during reading a file

  • Thread starter Thread starter bnob
  • Start date Start date
B

bnob

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 ??
 
bnob said:
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 ??
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,

I had read that something so I knew there was something with my answer.
http://msdn.microsoft.com/library/d.../html/frlrfsystemiofilestreammemberstopic.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
 
Cor Ligthert said:
Peter,

I had read that something so I knew there was something with my answer.
http://msdn.microsoft.com/library/d.../html/frlrfsystemiofilestreammemberstopic.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...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top