fstream is mean to me :( won't write

Thanks in advance for any help yall can give me.
I'm trying to figure out why the .write function and "<<" thingy don't seem to work for me to write to an already existing text file using fstream.

I've made a test program and tried to make it as simple as possible with no unnecessary fluff, other than some lines like "cout << fstream.tellp();" to check the position of the put pointer at certain times (in an attempt to debug.)

This program is supposed to open a file "sample.txt" with an fstream object, read a line from the file, then write some text to the same file, then close it. Not incredibly useful or anything, just made it to see what I am doing wrong. When the program is executed, data is read from the file ok, but the file's content remains unchanged from what it was before prog ran.

I hate to post ALL the unnecessary bits of source, but I'm not sure whats wrong so...anyway here's the code:

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main()
string got_line, put_line;

fstream put_data("sample.txt", ios::in || ios::out);
if (put_data.is_open()) {
cout << "File opened to read and write. (LOL! as if)\n";
else {
cout << "Error opening file.\n";

getline(put_data, got_line); // reads a line, does just fine..
cout << got_line << "\n";
cout << "Put pointer location: " << put_data.tellp() << "\n";

put_data.seekp(0, ios_base::beg); // put set to 0, still lookn good
cout << "Put pointer location: " << put_data.tellp() << "\n";

put_data.write("SMURF YOU",9); // hmm... something happens here.
cout << "Put pointer location: " << put_data.tellp() << "\n";


cout << "File closed.\n";
return 0;

ok and there is also a file: "sample.txt" with the following content:

This line should be read then passed to cout.
This line isn't necessary.

...and here's the readout from dos prompt I get when program runs:

File opened to read and write. (LOL! as if)
This line should be read then passed to cout.
Put pointer location: 47
Put pointer location: 0
Put pointer location: -1
File closed.
Press any key to continue

So, why can I read from this file but not write to it? The put pointer is set to 0 before I call the write function (have also tried some small non zero values as offset and setting to end) but after I use .write the position is set to -1(wtf?)

Also, I've read lots of places that when using an fstream the get and put pointers are seperate, and operate independently, but whenever I check the values of .tellg() and .tellp() they are ALWAYS the same at any time, even if I just changed the position of just one... is that normal?

Any input will be much appreciated
- c plus n()()b

p.s. I have tried replacing the .write line with put_data << "string" << flush;
This didn't work any better than .write
The problem is you should be using | to list the ios:: statements in the fstream constructor, || is the logical or symbol.

You may also be hitting the .eof() (end of file) flag when you read in a line from the file (depends how big your file is) which will mean the seekp will not work. You will need to use put_data.clear() to clear any of those flags in that case to make sure operation on the file still work.
Awesome! Thanks James, problem solved.
and so quickly.. this was my first post.

much appreciated, freind
Never seen it done that way, so I won't comment.

However this way works.

#include <iostream>
#include <fstream>

using namespace std;

ofstream outfile ("output.txt");

int main()

outfile << "This is a test " << endl;

    return 0;
Thanks Sam my issue was using the "||" or symbol instead of "|" in the fstream constructor. Once that was fixed, .write worked just fine.
Long story short, I'm building a program that will have a file open, read lines from that file, perform operations and functions on data drawn from the file, then modify some things from certain lines in the same file. Goes without saying it'd be a real pain to open/close the file thousands of times, once every time I need to read a line, then change a line.
Topic archived. No new replies allowed.