havent gone over fstream.h in class yet, and im overly curious on how to use ifstream so that i can take in data from a file besides the first word
Printable View
havent gone over fstream.h in class yet, and im overly curious on how to use ifstream so that i can take in data from a file besides the first word
http://www.cplusplus.com/ref/iostream/ifstream/ from http://www.cplusplus.com/ref/
has a good reference of ifstream, and other iostreams.
You can think of ifstream acting the same way that cin does.
you can use >> and getline on an ifstream object. But you have to check if your at the end of the file
ifstream::eof() will return true if you are at the end of the file.
ifstream::fail() will return true if the last action failed.
This code will echo a text file to the screen.
ifstream input;
input.open("somefile");
if( input.fail() )
{
cout << "Error opening somefile";
return 0;
}
char buffer[100];
while( ! input.eof()
{
input.getline(buffer,99,'\n');
cout << buffer << endl;
}