473,387 Members | 1,760 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Write during reading a file

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
4 1127

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

"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Wolfgang Kaml | last post by:
Hello All, I have been working on this for almost a week now and I haven't anything up my sleeves anymore that I could test in addition or change.... Since I am not sure, if this is a Windows...
2
by: jeff_zhang446 | last post by:
Hi, I try to write some data structure into a file as below and would appreciate if someone can give me some advice here. row(1) 100 1 0 0 0 0 0 0 0 1 row(2) 150 5 0 0 0 0 0...
4
by: georges the man | last post by:
hey guys, i ve been posting for the last week trying to understand some stuff about c and reading but unfortunaly i couldnt do this. i have to write the following code. this will be the last...
2
by: KatieB | last post by:
I am so totally new at this it is not funny. I am desiging a database for my work and want to know, what to me should be the simplest thing, how to write an expression that automatically puts the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.