How do you stor the contents of a textfile in a string?

This is a discussion on How do you stor the contents of a textfile in a string? within the C++ Programming forums, part of the General Programming Boards category; Here is what i came up with: Code: #include <fstream> #include <iostream> using namespace std; int main() { string str; ...

  1. #1
    Registered User
    Join Date
    Aug 2006
    Posts
    6

    How do you stor the contents of a textfile in a string?

    Here is what i came up with:

    Code:
    #include <fstream>
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
      string str;
      ifstream a_file( "C:\test.txt" );
      a_file>>str;
      cout<<str;
      cin.get();
    }
    it doesnt display anything. I have double and triple checked the test.txt and it does contain text. Could someone please help.

  2. #2
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,231
    Try this.

    getline(a_file, str, EOF); {EDIT] change EOF to '\0'.
    Last edited by siavoshkc; 08-24-2006 at 05:32 PM.
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Code:
    ifstream a_file( "C:\test.txt" );
    ->
    Code:
    ifstream a_file( "C:\\test.txt" );
    You had a tab in your string, so it was trying to open "C:<tab>est.txt". Remember to escape backslashes when you include them in a string.

    BTW, that program only reads one line.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  4. #4
    Registered User
    Join Date
    Aug 2006
    Posts
    6
    it only reads one line? how do you have it read the entire file?

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,319
    Use a stringstream, then copy the contents of the filestream's rdbuf() into that stringstream. Then get the string from the stringstream. rdbuf and stringstream might be enough to search for an example.

  6. #6
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,231
    it only reads one line? how do you have it read the entire file?
    After corrections?
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    7,319
    That doesn't compile after corrections.

  8. #8
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Do you need to have the whole file in memory at once? Or can you read in each line from the file, one at a time, until you've reached the end?
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  9. #9
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,088
    Since I'm here, here:
    Code:
    #include <iostream>
    #include <fstream>
    #include <sstream>
    #include <string>
    
    int main()
    {
        std::ifstream file(__FILE__);
        std::ostringstream oss;
        if ( oss << file.rdbuf() )
        {
            std::cout << oss.str();
        }
    }

  10. #10
    Registered User
    Join Date
    Aug 2006
    Posts
    6
    When I changed it it compiled.
    @jilou- Thanks, but could you explain it?

  11. #11
    Registered User
    Join Date
    Aug 2006
    Posts
    6
    This does not store the file in a single string though, correct?

  12. #12
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    There could be NULL's in a file and it could not be the EOF. I don't know how reliable this is, but making EOF a char and passing it as a delimiter to std::getline seems to work.

    Code:
    	std::ifstream f("c:\\_audioscrobbler.log");
    	std::string s;
    	std::getline(f, s, char(EOF));
    	std::cout << s;
    That does store the file in a single string actually.

    http://www.cplusplus.com/ref/iostream/ios/rdbuf.html

    And then an overloaded operator << that takes a streambuf

    Code:
    ostream& operator<< (streambuf& sb );

    ╔╗╔╦══╦╗╔╦══╦╗
    ║╚╝║╔╗║╚╝║╔╗║║
    ║╔╗║╠╣║╔╗║╠╣╠╣
    ╚╝╚╩╝╚╩╝╚╩╝╚╩╝

    codez http://code.google.com/p/zxcvbn/

  13. #13
    Registered User
    Join Date
    Aug 2006
    Posts
    6
    I figured out how to use jlou's meathod to store the file into a single string, btu I still dont understand it. Anyone have any recources for help?

  14. #14
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    http://www.cplusplus.com/ref/iostream/stringstream/

    You can write data out to a this stringstream, and it'll be just like writing data to the console except to this string buffer. Kind of analogous to sprintf and printf. So he writes the file to the buffer using operator << (streambuf &), the streambuf to the file being obtained through the call to rdbuf: http://www.cplusplus.com/ref/iostream/ios/rdbuf.html and then he gets the std::string from the stringstream with the call to str http://www.cplusplus.com/ref/iostrea...tream/str.html

    ╔╗╔╦══╦╗╔╦══╦╗
    ║╚╝║╔╗║╚╝║╔╗║║
    ║╔╗║╠╣║╔╗║╠╣╠╣
    ╚╝╚╩╝╚╩╝╚╩╝╚╩╝

    codez http://code.google.com/p/zxcvbn/

  15. #15
    Registered User
    Join Date
    Aug 2006
    Posts
    6
    thanks, it isnt quite as complicated as it looks at first glance.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. OOP Question DB Access Wrapper Classes
    By digioz in forum C# Programming
    Replies: 2
    Last Post: 09-07-2008, 04:30 PM
  2. Custom String class gives problem with another prog.
    By I BLcK I in forum C++ Programming
    Replies: 1
    Last Post: 12-18-2006, 02:40 AM
  3. Linked List Help
    By CJ7Mudrover in forum C Programming
    Replies: 9
    Last Post: 03-10-2004, 09:33 PM
  4. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 02:23 PM
  5. creating class, and linking files
    By JCK in forum C++ Programming
    Replies: 12
    Last Post: 12-08-2002, 01:45 PM

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