Thread: ifstream, ofstream

  1. #1
    Registered User
    Join Date
    Nov 2001
    Posts
    255

    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?
    hooch

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    7
    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!!!

  3. #3

  4. #4
    Registered User
    Join Date
    Nov 2001
    Posts
    255
    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?????????
    hooch

  5. #5
    S­énior Member
    Join Date
    Jan 2002
    Posts
    982
    You use ifstream to read from files, not write to them.

  6. #6
    Registered User
    Join Date
    Nov 2001
    Posts
    255
    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????????
    hooch

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

  8. #8
    Unregistered
    Guest
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Ofstream... broke as a result of ifstream fix
    By tms43 in forum C++ Programming
    Replies: 3
    Last Post: 11-12-2006, 11:40 AM
  2. Capturing key presses
    By cgod in forum Windows Programming
    Replies: 6
    Last Post: 11-25-2004, 01:10 PM
  3. ofstream and ifstream for searching and writing
    By johnnyd in forum C++ Programming
    Replies: 6
    Last Post: 03-17-2003, 08:34 AM
  4. Replies: 9
    Last Post: 06-06-2002, 07:03 PM
  5. STL to: Cin, Cout, ifstream & ofstream
    By kuphryn in forum C++ Programming
    Replies: 3
    Last Post: 12-02-2001, 09:32 AM