I need help with <FSTREAM.H>

This is a discussion on I need help with <FSTREAM.H> within the C++ Programming forums, part of the General Programming Boards category; Could somebody please help me with <fstream.h>, I am a new programmer in C++ and I would aprreciate any help, ...

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    2

    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.

    Thankyou.

  2. #2
    Registered User
    Join Date
    Aug 2002
    Posts
    170
    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,

    Bonkey

  3. #3
    Registered User
    Join Date
    Apr 2002
    Posts
    142
    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, 05:49 PM
  2. #include <fstream.h>
    By bluehead in forum C++ Programming
    Replies: 4
    Last Post: 02-13-2002, 04:51 PM

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