fstream infile (argv[1]);
string x;
while(getline(infile, x));
infile << "XXXXXXXXXXXXXx";
How to read/write to files in C++ without Overwriting
I'm currently trying to open a text file for reading and writing.
I want to use fstream because I need to read AND write.
However, I don't want to overwrite anything.
I've seen all kinds of convoluted code that involves moving the cursor around, but I'm looking for something much simpler.
Does not write the X's to the end of the file.
What am I doing wrong?
iostreams have two cursors: one for reading, one for writing. The simplest and most intuitive way to write at the end of the file is to place the write cursor there:
stream.seekp(0,ostream::end);
Alternatively, you may create a file stream with the cursor already positioned at the end by using the app flag when opening the file.
stream.seekp(0,ostream::end);
Alternatively, you may create a file stream with the cursor already positioned at the end by using the app flag when opening the file.
Did you try to use the app flag when opening the fstream?
fstream infile (argv[1], ios::app);infile << "XXXXXXXXXXXXXx";
But if I do these things, then I cannot read the file because the cursor will be at the end of the file.
The write cursor will be at the end of file. The read cursor is wherever you set it to be, and is entirely independent of the former.
fstream infile ("testFile", ios::app);
infile << "XXXXXXXXXXXXXx";
Gives no compile time error whatsoever.
But it does absolutely nothing to testFile.
infile << "XXXXXXXXXXXXXx";
Gives no compile time error whatsoever.
But it does absolutely nothing to testFile.
This is the closest I can get
string line;ifstream fin("testFile");ofstream fout("testFile", ios::app);while(getline(fin, line));fout << "XXXXXXXXXXXXXx";
I managed to both read and write from a fstream object by poking into the filebuf member:
It seems to me though that using two different objects for reading and writing is a better way.
For some reason the operator << and >> does not work for the fstream object.
Maybe someone else knows more about that.
#include <fstream>#include <iostream>using namespace std;int main() { char ch; fstream filestr; filebuf *pbuf; pbuf=filestr.rdbuf(); pbuf->open("testFile", fstream::in | fstream::out); while(pbuf->sgetc() != EOF) { ch = pbuf->sbumpc(); cout << ch; } pbuf->sputn("XXXXXXXXXX", 10); pbuf->close(); return 0;}
It seems to me though that using two different objects for reading and writing is a better way.
For some reason the operator << and >> does not work for the fstream object.
Maybe someone else knows more about that.
I am using two seperate objects now.
Was just hoping I could do it with one object. But even if there is a way it's too messy.
Thanks for your help.
Was just hoping I could do it with one object. But even if there is a way it's too messy.
Thanks for your help.
You want to read from a file and write to another?
Simplest thing I can think of
istream in("testFile", ios::app);ostream out("outputfile.txt");string x;in >> x;while(!in.eof()){out << x;}
Simplest thing I can think of
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement