Thread: Writing to/Reading a file

  1. #1
    Registered User
    Join Date
    Sep 2001

    Writing to/Reading a file

    Yes, I am a newbie.....

    I would like to know how to get my program to save data to be retrieved later....Meaning after the program has been closed and opened several times. I know this can be done by either using the registry or writing to/reading a text file (or an INI file.

    So, I was wondering, how do you write to/read a text file?
    Jacob Sheehy

  2. #2
    Registered User matheo917's Avatar
    Join Date
    Sep 2001
    in the strandard header file <fstream.h>

    you can declare an object of "ifstream" which stands for input file stream, which is used for reading into your program anything from a file, i.e.:

    ifstream fin("c:\\text.txt");

    the previous statement is instantiating an object of "ifstream" and opening a file for (reading) this reading is based on text file, you can also open a file in a binary mode i.e.:

    ifstream fin("c:\\text", ios::binary);

    there's a lot of different ways of reading in data from a file so i won't get into that i will just give you one example....

    in a standard <iostream.h> and <string.h>

    string str;
    getline(fin, str, '\n');

    although i tell you to include <iostream.h> , you don't have to when you work with file streams, b/c <fstream.h> includes already a definition of <iostream.h> in itself...

    you have to make sure you close the file when you're done by typing:

    otherwise the file will remain open until the end of the file (eof())

    to write into a file by instatiating an outpu stream object i.e:

    ofstream fout("c:\\text.txt");

    previous line opens a file for output "writing", also earases current file contents

    obviously you can specify modes, as well.. i.e.
    if you don't add to current file contents w/o ereasing anything

    ofstream fout("c:\\text.txt, ios::app);

    for example if you want write your name into a file...

    string name = "orufet";
    fout << name;

    make sure you won't forget to close the file....


    the reason why i am stressing it is because in most of the compiler cases it allows you to have 2 file open at a time, one for input and one for output

    that should get you started, work from here, good luck...


  3. #3
    Registered User
    Join Date
    Sep 2001
    Thank you very much, that's exactly what I needed!
    Jacob Sheehy

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Data Structure Eror
    By prominababy in forum C Programming
    Replies: 3
    Last Post: 01-06-2009, 09:35 AM
  2. opening empty file causes access violation
    By trevordunstan in forum C Programming
    Replies: 10
    Last Post: 10-21-2008, 11:19 PM
  3. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 12:36 PM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. Simple File encryption
    By caroundw5h in forum C Programming
    Replies: 2
    Last Post: 10-13-2004, 10:51 PM