Reading text file into a string obj

This is a discussion on Reading text file into a string obj within the C++ Programming forums, part of the General Programming Boards category; I need to read a text file into a set of tokens. To do this, I would like to read ...

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

    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
    21,794
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

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

  4. #4
    Registered User
    Join Date
    Aug 2005
    Posts
    197
    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
    197
    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,328
    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, 03: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

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