|
-
February 25th, 2006, 12:46 PM
#1
More strings and ifstream questions ...
Hey guys.
Not long ago I started this thread with a task
I wanted done.
http://www.codeguru.com/forum/showthread.php?t=376601
I have a fully functional program right now and I'm even "porting" it to VC++ to make use of a GUI.
But before I do this, is there a way to open a file as input and as output?!?!?
For example can I say:
ifstream inW("text.txt");
ofstream outW("text.txt");
without any problems??
I'm asking this because, let's say that I ran my program which opens one file and records changes to another file, like:
ifstream inFile("entrada.txt");
ofstream outFile("salida.txt");
//Process input file and records to output
inFile.close();
outFile.close();
Now, let's say that I want to go back to a specific line in "salida.txt".
Can I open "salida.txt" as input, go to a specific line (let's say line #7), modify this line, and save the output WITHOUT modifying anything else
on the file and just this line?!?!
Thanks.
-
February 25th, 2006, 12:56 PM
#2
Re: More strings and ifstream questions ...
The answer to the question "can I open a file in both input and output?" is yes.
fstream is a class derived from iostream which is itself derived from both istream and ostream.
fstream can be used to do both input & output at the same time (with a single buffer) in a file.
But, if you want to modify a line without affecting other lines, you probably cannot directly.
Actually, you can modify text data, if and only if the new text overrides exactly the old.
It means that the newly created line must have exactly the same size than the old one.
If you change the size of the line, you'll need to copy all the characters following the line.
However, if the file format is yours, then you can avoid that problem by providing a limit to lines lengths, and fills the missing characters with null characters, or space characters.
But, I doubt you need that.
Generally, a text file can be treated by reading its full content, then doing few or much processing on an adequate internal representation, and then writing the full contents back to the file.
Last edited by SuperKoko; February 25th, 2006 at 12:59 PM.
"inherit to be reused by code that uses the base class, not to reuse base class code", Sutter and Alexandrescu, C++ Coding Standards.
Club of lovers of the C++ typecasts cute syntax: Only recorded member.
Out of memory happens! Handle it properly!
Say no to g_new()!
-
February 26th, 2006, 11:41 AM
#3
Re: More strings and ifstream questions ...
Ok, so I can read and write to the same file but I wont
be able to make a change to just one line if the size is changed... (which it will
probably change). hmmmm...
the thing is that I process the data in a procedure and a counter
number is increased. Then at last when all of the processing is finished
I would love to use that counter value to fill one of the lines in the document.
But it turns out that the line I want to modify is at the beginning of the document!

So I will probably have to make another function to read the file,
copy the contents except for the new line with the new value, and keep copying
the other 5000 lines! 
Thanks for the comments.
-
February 26th, 2006, 01:18 PM
#4
Re: More strings and ifstream questions ...
If the line modification consists of changing the value of an integer, you can avoid copying the whole file by using a fixed length format for the numeric value, such as:
Before processing
Code:
0000000042
file data
After processing
It does not modify the size of the line.
"inherit to be reused by code that uses the base class, not to reuse base class code", Sutter and Alexandrescu, C++ Coding Standards.
Club of lovers of the C++ typecasts cute syntax: Only recorded member.
Out of memory happens! Handle it properly!
Say no to g_new()!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|