Thread: Read file into variable

  1. #1
    Registered User
    Join Date
    Mar 2005
    Posts
    27

    Read file into variable

    Hello,

    I started learning C++ yesterday and have been working through the tutorials. I have come to the part regarding File I/O and am a little stuck...

    Code:
     ifstream ifile("test.txt");
     ifile >> old_ans;
     ifile.close();
    
     cout << "File contents:\n\n" << old_ans;
    The above is a snippet from my code, the problem is cout'ing old_ans only outputs the first word from the file, how should i go about outputting the whole file contents?

    My previous experience is with PHP in which i would just use:

    Code:
    $var = file_get_contents('text.txt');
    etc
    But being a c++ newbie i am a little stuck on a simple problem.

    Thank you for any help or advice you can give me.

  2. #2
    Registered User Micko's Avatar
    Join Date
    Nov 2003
    Posts
    715
    See if this can satisfy you
    Code:
    #include <cstdlib>
    #include <iostream>
    #include <fstream>
    
    using namespace std;
    
    int main()
    {
       ofstream o("test.txt");
       o<<"Test"<<endl<<"And more test"<<endl;
       o.close();
       
       ifstream in("test.txt");
       cout<<in.rdbuf();
       system("pause");
    }
    Gotta love the "please fix this for me, but I'm not going to tell you which functions we're allowed to use" posts.
    It's like teaching people to walk by first breaking their legs - muppet teachers! - Salem

  3. #3
    Registered User
    Join Date
    Mar 2005
    Posts
    27
    Thank you Micko, that worked nicely

  4. #4
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    Hi,

    You need to know that the ">>" operator has a special method of reading input: it skips all leading whitespace(spaces, tabs, newlines) until it finds something to read, then it starts reading input, and then it stops reading input when it encounters any whitespace.

  5. #5
    Registered User
    Join Date
    Mar 2005
    Posts
    27
    Thank you for the information 7stud, that explains the behaviour of my original code.

    On the bright side, a mistake made leads to a lesson learned.

  6. #6
    Registered User Micko's Avatar
    Join Date
    Nov 2003
    Posts
    715
    If you don't screw up, you don't learn! Its just that simple!
    Gotta love the "please fix this for me, but I'm not going to tell you which functions we're allowed to use" posts.
    It's like teaching people to walk by first breaking their legs - muppet teachers! - Salem

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 12:36 PM
  2. Basic text file encoder
    By Abda92 in forum C Programming
    Replies: 15
    Last Post: 05-22-2007, 01:19 PM
  3. read in csv file
    By gums in forum C Programming
    Replies: 5
    Last Post: 05-10-2007, 07:38 AM
  4. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 10:46 AM
  5. what does this mean to you?
    By pkananen in forum C++ Programming
    Replies: 8
    Last Post: 02-04-2002, 03:58 PM