How to read/write to files in C++ without Overwriting

Started by
8 comments, last by bluefox25 16 years, 11 months ago
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.

fstream infile (argv[1]);
string x;
while(getline(infile, x));
infile << "XXXXXXXXXXXXXx";

Does not write the X's to the end of the file. What am I doing wrong?
Everyone hates #1.That's why a lot of idiots complain about WoW, the current president, and why they all loved google so much when it was new.Forget the fact that WoW is the greatest game ever created, our president rocks and the brainless buffons of America care more about how articulate you are than your decision making skills, and that google supports adware, spyware, and communism.
Advertisement
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.
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.
Everyone hates #1.That's why a lot of idiots complain about WoW, the current president, and why they all loved google so much when it was new.Forget the fact that WoW is the greatest game ever created, our president rocks and the brainless buffons of America care more about how articulate you are than your decision making skills, and that google supports adware, spyware, and communism.
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.
Everyone hates #1.That's why a lot of idiots complain about WoW, the current president, and why they all loved google so much when it was new.Forget the fact that WoW is the greatest game ever created, our president rocks and the brainless buffons of America care more about how articulate you are than your decision making skills, and that google supports adware, spyware, and communism.
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:

#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.
Everyone hates #1.That's why a lot of idiots complain about WoW, the current president, and why they all loved google so much when it was new.Forget the fact that WoW is the greatest game ever created, our president rocks and the brainless buffons of America care more about how articulate you are than your decision making skills, and that google supports adware, spyware, and communism.
You want to read from a file and write to another?
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