Thread: Reading from txt

  1. #1
    Registered User
    Join Date
    Jan 2005
    Posts
    25

    Reading from txt

    how can you make a string equal to a stream from a txt file?
    Code:
    #include <iostream>
    #include <string>
    #include <fstream>
    using namespace std;
    int main(){
    string yay;
    ofstream a ("yay.txt");
    a<<"stringystring!";
    istream at ("yay.txt");
    yay=getline(a, line);
    }
    my code is somewhat like the above

  2. #2
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,273
    Code:
    string MyString;
    
    ifstream in ("whatever.txt");
    getline( in, MyString );
    
    cout<< MyString;
    There's no error checking here. I'd recommend a

    if ( in ) //

    or something similar

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >how can you make a string equal to a stream from a txt file?
    I'm not sure I understand what you want. Do you want to fill a string with the entire contents of a file? If so, this should work for you:
    Code:
    #include <fstream>
    #include <iostream>
    #include <sstream>
    
    int main()
    {
      std::ifstream in ( "test.txt" );
    
      if ( in ) {
        std::stringstream ss;
    
        ss<< in.rdbuf();
    
        std::cout<< ss.str();
      }
    }
    My best code is written with the delete key.

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Note that the str() function in that code returns the string that you can store in a variable if you'd like. The last time I recommended this strategy I failed to point that out and it led to some confusion.

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    25
    The point is to store a name in a txt file, and retrieve it later, even if there are other things in the file

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Then your code is on the right track. Go ahead and try it (and fix the little mistakes) and see if it works.

    One thing to watch for is if you output the string to the ofstream, you should probably close that ofstream before opening the file and reading the string with the ifstream. Also, you may want to consider adding a newline to the end of the string if you really are going to output only that to the file.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. problem reading numbers in a txt file
    By nimamc in forum C Programming
    Replies: 3
    Last Post: 06-03-2009, 02:35 PM
  2. Reading txt files
    By Tbrez in forum C Programming
    Replies: 6
    Last Post: 03-10-2009, 03:40 PM
  3. Replies: 7
    Last Post: 02-02-2009, 07:27 AM
  4. Reading a txt file into a struct problem
    By Swerve in forum C++ Programming
    Replies: 10
    Last Post: 03-19-2008, 12:56 AM
  5. reading the nth item from a txt file
    By Opel_Corsa in forum C++ Programming
    Replies: 1
    Last Post: 11-18-2006, 05:58 PM