i think u demonstrated how to use ifstream but i didnt quite catch it?
i think u demonstrated how to use ifstream but i didnt quite catch it?
hooch
#include <fstream.h> //I have an older compiler
int main()
{
//declare variables to be used in program
int dummy;
int data = 123;
//obtain a filename for the file you wish to use. If the file doesn't
//exist the compiler will create it for you and locate it in the same
//directory as the executable file if you use the following syntax.
char fileName[20] = "dataFile.txt";
//declare ofstream, open it, and associate with the above file all
//in one line
ofstream fout(fileName);
//use ofstream to write to the file you just created
fout << data;
//at some point open dataFile.file and see that data actually
//written to it. Maybe after program completes its run.
//close the ofstream for good practice
fout.close();
//now declare ifstream to read from file to program
ifstream fin(fileName);
//and use fin to store information from file in dummy
fin >> dummy;
//close ifstream to be tidy
fin.close();
//print out dummy to prove you successfully read file
cout << dummy;
//close program
return 0;
}
There are a multitude of variations on this simple process including appending data to file already in existence rather than overwriting file contents, storing data in file as binary rather than text, using istream methods other than >>, reading/writing entire structs/objects in one fell swoop, etc. Getting a good book on C++ to review many of the possibilities is a VERY good idea. Some are available on line for your use without fee, so there is no good reason not to have available "at your finger tips."
There is a great tutorial on this in the tutorial section of the main site. Try http://www.cprogramming.com/tutorial.html There are also several answers to this question on the FAQ board. However, I recommend the tutorial, the webmaster's real good at explaining things like this.
I agree...Originally posted by Betazep
There is a FAQ on file IO. Use it...
Blue
i tryed taht but when i opened my file i made it didnt have data in it at all i thought it was supposed to have data in it?
hooch