Thread: how do i read data from a file?

  1. #16
    Registered User
    Join Date
    Nov 2001
    i think u demonstrated how to use ifstream but i didnt quite catch it?

  2. #17
    #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

    //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

    //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."

  3. #18
    Lead Moderator kermi3's Avatar
    Join Date
    Aug 1998
    There is a great tutorial on this in the tutorial section of the main site. Try 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.

    If you're new to the boards, welcome and reading this will help you get started.
    Information on code tags may be found here

    - Sandlot is the highest form of sport.

  4. #19
    _B-L-U-E_ Betazep's Avatar
    Join Date
    Aug 2001
    Originally posted by Betazep
    There is a FAQ on file IO. Use it...
    I agree...

  5. #20
    Registered User
    Join Date
    Nov 2001
    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?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. xor linked list
    By adramalech in forum C Programming
    Replies: 23
    Last Post: 10-14-2008, 10:13 AM
  2. Read data from file - C program ?
    By Pawan Sangal in forum C Programming
    Replies: 2
    Last Post: 08-22-2008, 04:28 AM
  3. gcc link external library
    By spank in forum C Programming
    Replies: 6
    Last Post: 08-08-2007, 03:44 PM
  4. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM
  5. gcc problem
    By bjdea1 in forum Linux Programming
    Replies: 13
    Last Post: 04-29-2002, 06:51 PM