Thread: getting data from a file

  1. #1
    Unregistered
    Guest

    Arrow getting data from a file

    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

  2. #2
    Registered User
    Join Date
    Dec 2001
    Posts
    194
    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;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. opening empty file causes access violation
    By trevordunstan in forum C Programming
    Replies: 10
    Last Post: 10-21-2008, 11:19 PM
  2. Replies: 3
    Last Post: 02-26-2008, 02:12 PM
  3. Can we have vector of vector?
    By ketu1 in forum C++ Programming
    Replies: 24
    Last Post: 01-03-2008, 05:02 AM
  4. Bitmasking Problem
    By mike_g in forum C++ Programming
    Replies: 13
    Last Post: 11-08-2007, 12:24 AM
  5. Writing and modifying data in a file
    By Micko in forum C Programming
    Replies: 2
    Last Post: 02-17-2005, 03:42 AM