Thread: I need help with <FSTREAM.H>

  1. #1
    Registered User
    Join Date
    Oct 2002

    Question I need help with <FSTREAM.H>

    Could somebody please help me with <fstream.h>, I am a new programmer in C++ and I would aprreciate any help, if you are willing to help please read on!

    I need to know how you get variables from a .TXT file and use them in a program, for example:
    Say i have declared int x - and i want to get the value of x from a text file, how is this done?

    My Second problem is that i only know how to read in one word at a time from a .TXT, like in the tutorial.......

    #include <iostream.h>
    #include <fstream.h>
    int main()
    char str[10]

    ifstream text("example.txt");
    text >> str;
    cout << str;

    .......Or however it is. Could I please be told how to get the whole text document printed to the screen at once.


  2. #2
    Registered User
    Join Date
    Aug 2002
    To get the whole file you have to read a line at a time until you reach the end. That may sound simplistic, but that's how it is done.

    The other question is easy too. Just like you already read from the file, just put your varirable there to receive the input. Just like you are already doing in this code. You are reading in a string variable from a file. To read an int:

    int x;
    ifstream fin("varfile");
    fin >> x;
    Best Regards,


  3. #3
    Registered User
    Join Date
    Apr 2002
    you could make your very own parser if you want, or you should know the structure/format of your text file, this way, you'll know when your integer etc., will come,

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. About "fstream" in <fstream.h> and <fstream>
    By L.O.K. in forum C++ Programming
    Replies: 5
    Last Post: 01-08-2005, 06:49 PM
  2. #include <fstream.h>
    By bluehead in forum C++ Programming
    Replies: 4
    Last Post: 02-13-2002, 05:51 PM