how do i read data from a file?

This is a discussion on how do i read data from a file? within the C++ Programming forums, part of the General Programming Boards category; i think u demonstrated how to use ifstream but i didnt quite catch it?...

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

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

  3. #18
    Lead Moderator kermi3's Avatar
    Join Date
    Aug 1998
    Posts
    2,595
    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.
    Kermi3

    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
    Posts
    1,412
    Originally posted by Betazep
    There is a FAQ on file IO. Use it...
    I agree...
    Blue

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

Page 2 of 2 FirstFirst 12
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, 11:13 AM
  2. Read data from file - C program ?
    By Pawan Sangal in forum C Programming
    Replies: 2
    Last Post: 08-22-2008, 05:28 AM
  3. gcc link external library
    By spank in forum C Programming
    Replies: 6
    Last Post: 08-08-2007, 04:44 PM
  4. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 09:39 AM
  5. gcc problem
    By bjdea1 in forum Linux Programming
    Replies: 13
    Last Post: 04-29-2002, 07:51 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21