-
About io stream!
i'm writing a program for the io stream one.i wanna to save a file after i wrote all the output inside the file. Which i called "Save"? and then,i able to save all the output in that file i created called "Save". but for now i still can't write this simple program..... pls give some direction on how to do it? thanks
-
When you open a file for output (write to file) all writting operations happen on demand. You don't save the file. You simply open it, write to it and close it.
Just to guide you on what you should be reading about, the following code opens a new file, writes 2 lines and closes it.
Code:
#include <fstream>
#include <iostream>
using namespace std;
int main(void)
{
//creates a pointer to a file. ofstream since we will be using '<<' in order to write to it.
ofstream pfile;
//open/create the file. File is named teste.txt. 'ios:out' because we want to write to the file
//If it already exists, it's created anew and everything on it is lost.
//If it doesn't exist, it is created.
pfile.open("teste.txt", ios::out);
//the following tests if the file exists. IMPORTANT!!
if(!pfile)
{
cout << "Error opening file. Exiting..."<< endl;
system("pause");
exit(1);
}
//now lets just write to the file. 3 lines in all.
pfile << "This is line 1" << endl;
pfile << "This is line 2" << endl;
pfile << "This is line 3";
//Close the file when you no longer need it. IMPORTANT!!
pifile.close();
return 0;
}
Alas, you just created a file. It should be on the same folder as your source. From here just try and grab some tutorial on file I/O. Don't use the one on this site. It's buggy :(
[EDIT]
Just replace the smiley you see on the code for a ':' followed by an 'o' (that's the letter o)
[/EDIT]
-