-
ifstream, ofstream
is this correct?
ofstream:used to output data into a file
ifstream:used to read data from a file
and an example of ifstream at work would help since i still cant seem to figure it out really confuses me for sum reason but it seems like an important concept that i need to master?
-
yo....make sure you use
#include <fstream>
then you can declare things like
ofstream out;
ifstream in;
and use a file for your in and out by opening it
out.open("file name here.extension");
or
in.open("file here.ext");
and use them like cin and cout
out << array[2];
im pretty sure this is right.. Correct me if I'm wrong guys!!!
-
-
yea thats what i thought ifstreamwas too but then again how do u allow for in>>variable;
to input to a filehwhen u input stuff to a file with ofstream ya know?????????
-
You use ifstream to read from files, not write to them.
-
how do i read from files this what i thought it did but i havent been able to figure it out it really confuses me alot and ive tryed the faq on this page and even asked the webmaster but havent been doing to well with is there like a simple trick to this to make it easy????????
-
include fstream
ok, ofstream is used to write to a datfile
so if i wanted to write the names of a bunch of students in a data file I would code:
cout << "Enter name: \n";
string name;
cin >> name;
// this opens an instance of the stream called write and ios::app tells it to write to the end of the file without, it would overwrite each time
ofstream write("test.txt", ios::app);
// put name in the stream called write, so name is now in test.txt
write << name << " ";
To read use:
// open stream to read called read
ifstream read("test.txt");
//create a var for use to outptu
string output;
// this will output the entire datafile, basically it reads from the datafile while there is stuff to read and it seperates words by spaces
while (read >> output)
{
cout << output << endl;
}
Hope this helps u not sure if i answered your question
-
go to this website and read the entire chapter, #16, called Streams, from Liberty, Teach yourself C++ in 21 d. It is a little out of date in that it uses h extensions, but that is minor.
http://www.stud.fim.ntnu.no/~oystesk/CPP/htm/ch16.htm