Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

file open modes

Status
Not open for further replies.

Sherak

Programmer
Sep 1, 2003
83
GB
Hi

Does anyone know the the correct file open mode to replace a line in a stream. ios::????

ie I have a file like so:

one
two
three
four

and I want to replace one with a diferent string ?..

regards,

 
I'm assumming that you want to open the file for both input and output. Therefore, you need to use fstream instead of ofstream or ifstream. I assume you've already guessed that since now you want to use ios manipulators. If you use
Code:
ios::out
without another manipulator, the file will open for truncation. (The same as
Code:
ios::trunc
.) Have you tried just
Code:
ios::in|ios::out
?

You could do try
Code:
ios::in|ios::app
or
Code:
ios::in|ios::out|ios::ate
. I haven't tried just the
Code:
ios::ate
by itself but it might work, too.

The ios::app opens an output file for appending only. Ios::ate opens a file (input or output) for appending. Both seek to the end of the file so you will have to position to the beginning of the file to do any kind of search.

One thing to note: if you don't declare standard namespace, you may have to use
Code:
std::ios::in|std::ios::out[\code], etc.

Bruce Eckel is working on a revision to his book [i]Thinking in C++[/i]. You can download a rought draft at [URL unfurl="true"]http://64.78.49.204/[/URL] . It talks about IOStreams in it.

James P. Cottingham
[COLOR=blue]
When a man sits with a pretty girl for an hour, it seems like a minute. But let him sit on a hot stove for a minute and it's longer than any hour. That's relativity.
[/color][tab][tab]Albert Einstein explaining his Theory of Relativity to a group of journalists.
 
Thanks for the reply,

Yes I've tried all those handles, they all seem to either add to the end, insert or delete the contents of the file. Thanks for the link on the book though, also I'll try the namespace way.

Thanks again.
 
If i'm not mistaking your problem is that you has a file with strings in and want to replace any given string with another one.......
If that is the case you must know that the strings are "packed" like this: "one\rtwo\rtree\rfour\r" or whatever character in between. This means that it's not just indexing and writing a new string with a DIFFERENT length, if the lengths are equal it's ok (i.e "one\r" -> "One\r" is OK, "one\r"->"Twelve\r" is trouble).
This means that in case SOME or all of the strings can differ i length You need to read, change and save in another file, and when all is done, delete the original file and rename the new to the same name.

Totte
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top