Collecting text and putting in string

This is a discussion on Collecting text and putting in string within the C++ Programming forums, part of the General Programming Boards category; Hey! This time, I'm trying to collect the text (ALL text) from a text file, and put it in a ...

  1. #1
    Registered User
    Join Date
    Oct 2006
    Posts
    118

    Collecting text and putting in string

    Hey!

    This time, I'm trying to collect the text (ALL text) from a text file, and put it in a string that I can print out later. Here's what I've come up with:

    Code:
    #include <iostream.h>
    #include <stdio.h>
    #include <fstream.h>
    
    using namespace std;
    
    string InfoCollectDataHolder;
    
    class ParseText
    {
        public:
            ParseText();
            ~ParseText();
            void Parse();
        protected:
            string InfoCollectDataHolder;
    };
    
    ParseText::ParseText()
    {
    
    }
    
    ParseText::~ParseText()
    {
    
    }
    
    void ParseText::Parse()
    {
        ifstream FileToParse("InfoCollect.txt");
        while (FileToParse)
        {
            getline (FileToParse, InfoCollectDataHolder);
        }
        FileToParse.close();
    }
    
    int main()
    {
        ParseText ParseExample;
        ParseExample.Parse();
        cout<<InfoCollectDataHolder;
        cin.get();
    }
    Hmm... doesn't seem to work right.
    Can somebody show me the err's of my ways?

    Thanks for your help!
    FlyingIsFun1217

  2. #2
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    You repeat to read into the same string and never store that string anywhere.
    Kurt

  3. #3
    Registered User
    Join Date
    Oct 2006
    Posts
    118
    Sorry, you're going to have to dumb that down some

    FlyingIsFun1217

    ---------

    Sorry, I think I see what you mean...

    FlyingIsFun1217...
    Last edited by FlyingIsFun1217; 04-07-2007 at 02:45 PM.

  4. #4
    Registered User Noir's Avatar
    Join Date
    Mar 2007
    Posts
    218
    Read each line into a buffer string and then append it to InfoCollectDataHolder:
    Code:
    string s;
    
    while ( getline( FileToParse, s ) ) {
      InfoCollectDataHolder += s;
    }
    This is a cool way to read a whole file in one shot too:
    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    #include <sstream>
    
    
    using namespace std;
    
    
    int main() {
      ifstream ifs( "test.txt" );
    
      if ( ifs ) {
        ostringstream oss;
        string s;
    
        oss << ifs.rdbuf();
    
        s = oss.str();
    
        cout << s;
      }
    }

  5. #5
    Registered User
    Join Date
    Oct 2006
    Posts
    118
    Removed the while loop, kept the contents. Still nothing.

    Thanks again!
    FlyingIsFun1217

  6. #6
    For Narnia! Sentral's Avatar
    Join Date
    May 2005
    Location
    Narnia
    Posts
    719
    Noir's second suggestion works perfectly, modify that to fit your needs.
    Videogame Memories!
    A site dedicated to keeping videogame memories alive!

    http://www.videogamememories.com/
    Share your experiences with us now!

    "We will game forever!"

  7. #7
    Registered User
    Join Date
    Oct 2006
    Posts
    118
    Will do
    Thanks everybody! I'll probably be back :P

    FlyingIsFun1217

  8. #8
    Registered User
    Join Date
    Oct 2006
    Posts
    118
    Code:
    #include <iostream>
    #include <stdio.h>
    #include <fstream>
    #include <sstream>
    
    using namespace std;
    
    string InfoCollectDataHolder;
    
    class ParseText
    {
        public:
            ParseText();
            ~ParseText();
            void Parse();
        protected:
            string InfoCollectDataHolder;
    };
    
    ParseText::ParseText()
    {
    
    }
    
    ParseText::~ParseText()
    {
    
    }
    
    void ParseText::Parse()
    {
        ifstream FileToParse("InfoCollect.txt");
        if (FileToParse)
        {
            ostringstream oss;
    
            oss << FileToParse.rdbuf();
    
            InfoCollectDataHolder = oss.str();
        }
    }
    
    int main()
    {
        ParseText ParseExample;
        ParseExample.Parse();
        cout<<InfoCollectDataHolder;
        cin.get();
    }
    This seems to not work either...
    Does it matter that I'm using Linux? I'm kinda new to developing on the platform, but I wouldn't think that there's anything to stop it from working right... :/

    FlyingIsFun1217

  9. #9
    Registered User
    Join Date
    Dec 2006
    Location
    Scranton, Pa
    Posts
    252
    Code:
    #include <iostream>
    #include <stdio.h>
    #include <fstream>
    #include <sstream>
    
    using namespace std;
    
    string InfoCollectDataHolder;
    
    class ParseText
    {
        public:
            ParseText();
            ~ParseText();
            void Parse();
        protected:
            string InfoCollectDataHolder;
    };
    
    ParseText::ParseText()
    {
    
    }
    
    ParseText::~ParseText()
    {
    
    }
    
    void ParseText::Parse()
    {
        
          ifstream FileToParse("InfoCollect.txt");
        if (FileToParse)
        {
            ostringstream oss;
    
            oss << FileToParse.rdbuf();
    
            InfoCollectDataHolder = oss.str();
            cout<<InfoCollectDataHolder;
        }
        }
    
    int main()
    {
        ParseText ParseExample;
        ParseExample.Parse();
        cin.get();
    }

  10. #10
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Code:
    InfoCollectDataHolder
    You have two of them.
    You are reading into ParseExample.InfoCollectDataHolder and printing the global one.
    Kurt

  11. #11
    Registered User
    Join Date
    Oct 2006
    Posts
    118
    Quote Originally Posted by Oldman47 View Post
    Code:
    #include <iostream>
    #include <stdio.h>
    #include <fstream>
    #include <sstream>
    
    using namespace std;
    
    string InfoCollectDataHolder;
    
    class ParseText
    {
        public:
            ParseText();
            ~ParseText();
            void Parse();
        protected:
            string InfoCollectDataHolder;
    };
    
    ParseText::ParseText()
    {
    
    }
    
    ParseText::~ParseText()
    {
    
    }
    
    void ParseText::Parse()
    {
        
          ifstream FileToParse("InfoCollect.txt");
        if (FileToParse)
        {
            ostringstream oss;
    
            oss << FileToParse.rdbuf();
    
            InfoCollectDataHolder = oss.str();
            cout<<InfoCollectDataHolder;
        }
        }
    
    int main()
    {
        ParseText ParseExample;
        ParseExample.Parse();
        cin.get();
    }
    Hmm... Not working for me. You got it to work?

    ZuK, I see what you mean. Whoops! Forgot to delete that... :P

    FlyingIsFun1217
    ----------------------------------------------
    Seems after deleting the class member InfoCollectDataHolder string, I still just get the same results. Shouldn't the class be using the global string and the main function read it too?

    Thanks again!
    FlyingIsFun1217
    Last edited by FlyingIsFun1217; 04-08-2007 at 12:37 PM.

  12. #12
    Registered User
    Join Date
    Dec 2006
    Location
    Scranton, Pa
    Posts
    252
    The code I posted worked fine. Maybe add #include <string> and I'd also add some code to handle a file error (perhaps your opening of the file is failing) since you have no idea as to whether or not that's occuring. As a test, simply assign some text to your string;

    Code:
    void ParseText::Parse()
    {
        
          ifstream FileToParse("InfoCollect.txt");
        if (FileToParse.good()) 
          {
                  ostringstream oss;
    
                  oss << FileToParse.rdbuf();
    
                 InfoCollectDataHolder = oss.str();
    
          }
    
            else if(FileToParse.bad())
               {
                 InfoCollectDataHolder= "This is a test of the emergency code system....";
               }
     
            cout<<InfoCollectDataHolder;
        }
        }
    Last edited by Oldman47; 04-08-2007 at 12:36 PM.

  13. #13
    Registered User
    Join Date
    Oct 2006
    Posts
    118
    Yep. Seems it's something with the text file:

    Code:
    void ParseText::Parse()
    {
        ifstream FileToParse("InfoCollect.txt");
    
        if (FileToParse)
        {
            ostringstream oss;
    
            oss << FileToParse.rdbuf();
    
            InfoCollectDataHolder = oss.str();
        }
    
        else
        {
            InfoCollectDataHolder = "Sorry, couldn't open file...";
        }
    
        cout<<InfoCollectDataHolder;
    }
    Is this maybe something with linux having permissions on the text file (I know thats kinda off topic, but...)? I have it set to read and write for all users... :/

    FlyingIsFun1217

  14. #14
    Registered User
    Join Date
    Dec 2006
    Location
    Scranton, Pa
    Posts
    252
    I don't know jack about Linux. Salem could probably answer that .....

  15. #15
    Registered User
    Join Date
    Oct 2006
    Posts
    118
    Where is Salem? I havn't seen him around much... Although thats not saying anything, because I havn't been around lately...

    FlyingIsFun1217

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. putting text onto the next line...lol...noobie questions
    By JOlszewski in forum C Programming
    Replies: 2
    Last Post: 01-24-2006, 04:02 PM
  2. Problems putting a text file into a matrix.
    By warny_maelstrom in forum C Programming
    Replies: 15
    Last Post: 01-22-2004, 06:33 PM
  3. Putting strings into an array from a text file.
    By JLan in forum C Programming
    Replies: 5
    Last Post: 11-20-2003, 07:34 PM
  4. Putting text into an edit box
    By eam in forum Windows Programming
    Replies: 5
    Last Post: 11-08-2003, 02:22 AM
  5. Putting CView text into a buffer
    By CodeMonkey in forum Windows Programming
    Replies: 4
    Last Post: 08-22-2002, 03:12 PM

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