Thread: Reading text file into a string obj

  1. #1
    Registered User
    Join Date
    Aug 2005
    Posts
    204

    Reading text file into a string obj

    I need to read a text file into a set of tokens. To do this, I would like to read the file into a string and then tokenize the string. How do I read a text file into one large string? Is it possible to tokenize the text file directly?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    How do I read a text file into one large string?
    One way is to use getline() to read it to a string, then concatenate the string to the result string.

    Is it possible to tokenize the text file directly?
    Yes, or at least I think it is a yes.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Code:
    std::ostringstream ostr;
    ostr << in_file.rdbuf();
    std::string contents = ostr.str();

  4. #4
    Registered User
    Join Date
    Aug 2005
    Posts
    204
    Quote Originally Posted by Daved View Post
    Code:
    std::ostringstream ostr;
    ostr << in_file.rdbuf();
    std::string contents = ostr.str();
    I don't know how to compile this into a program.

  5. #5
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Are you getting an error?
    Maybe you don't have this:
    Code:
    #include <sstream>

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by thetinman View Post
    I don't know how to compile this into a program.
    And exactly what are you trying to compile (or are you struggling to create a piece of source code that does this?

    If you are talking about tokenizing, I expected that you'd have basic understanding of how to write C++ code.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    Registered User
    Join Date
    Aug 2005
    Posts
    204
    I forgot the #include <sstream>. I know basic C++, but string and character manipulation is a bit of a weak spot.

    I've managed to copy the contents of a text file into a string. Now I would like to break up this string into a set of smaller strings. I was planning to use the strtok function, but it appears that this function will only operate on character arrays. Any suggestions are to how I should tokenize my text file (or my string)?

  8. #8
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    std::vector<char> should work just as well as a char array.

  9. #9
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    You could use a stringstream to tokenize your file contents, or find and substr. There are also more powerful tools available. Which is best depends on how you want to separate the text.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Inheritance Hierarchy for a Package class
    By twickre in forum C++ Programming
    Replies: 7
    Last Post: 12-08-2007, 04:13 PM
  2. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. Headers that use each other
    By nickname_changed in forum C++ Programming
    Replies: 7
    Last Post: 10-03-2003, 04:25 AM
  5. Ok, Structs, I need help I am not familiar with them
    By incognito in forum C++ Programming
    Replies: 7
    Last Post: 06-29-2002, 09:45 PM