flush the stream

S

Someonekicked

hi, i cant come to find a solution for a problem in my program, I made an
example of the problem;
in the example i made, the problem is if happy.dat does not exist, so
(!inData) will be true, and "here is the message" is sent to output to
happy.dat, and if u run the program, you will see that "here is the message"
is sent to happy.dat after the program ends, not be4 cout << "check"; i want
that "here is the message" be sent to file be4 seeing "check".
thx in advance for any suggestions , ive been struggling with this for long,
nothing i tried worked.



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


int main() {
string lol = "happy.dat";
ifstream inData;
ofstream outData;
inData.open(lol.c_str());
if (!inData)
{
outData.open(lol.c_str());
string message = "here is the message";
outData << message ;
}
cout << "check" << endl;
int check;
cin >> check;
return 0;
}
 
V

Victor Bazarov

Someonekicked said:
hi, i cant come to find a solution for a problem in my program, I made an
example of the problem;
in the example i made, the problem is if happy.dat does not exist, so
(!inData) will be true, and "here is the message" is sent to output to
happy.dat, and if u run the program, you will see that "here is the
message" is sent to happy.dat after the program ends, not be4 cout <<
"check"; i want that "here is the message" be sent to file be4 seeing
"check".
thx in advance for any suggestions , ive been struggling with this for
long, nothing i tried worked.



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


int main() {
string lol = "happy.dat";
ifstream inData;
ofstream outData;
inData.open(lol.c_str());
if (!inData)
{
outData.open(lol.c_str());
string message = "here is the message";
outData << message ;

Try adding

outData << flush;
outData.close();

although the effect is probably still platform-specific.
}
cout << "check" << endl;
int check;
cin >> check;
return 0;
}

V
 
S

Someonekicked

hey, thank YOU, it works. i tried fflush b4, it did not work, maybe becuz i
dont know how to use it, and for sure i tried close() alone, it did not work
either, anyway, thx a lot !
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top