string getline trouble

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • shendraeg
    New Member
    • Aug 2007
    • 4

    string getline trouble

    I'm trying to use fstreams to format/encode a text file in a way that is useful for another program of mine, and I came upon a little snag...
    The input file is formated as such:

    int (line count)
    string (line)
    string (line)
    string (line)
    etc...

    obviously, I start by reading in the integer and then proceed to use getline to try and get the strings, but the problem that I'm having is that getline won't pull the strings, and thus I'm not able to format them:

    Code:
    	int line_count(0);
    	string s;
    
    	int word_count(0);
    
    	in >> line_count;
    	out << line_count;
    
    	for(int i=0; i<line_count; ++i)
    	{
                                   ...
    		getline(in, s);
                                   ...
                    }
    in and out are the ifstream and ofstream, respectively. The string seems to come in as an empty string. Both outputting it and attempting to format it reveal this. It's quite likely that I'm missing/forgetting something stupidly simple, but it's been awhile since I've written anything with straight c++ code. Any insight would be appreciated.
  • sicarie
    Recognized Expert Specialist
    • Nov 2006
    • 4677

    #2
    I believe the '>>' operator leaves a '\n' char - did you try flushing the buffer? (An easy test would be to read it in twice, see if it came through the second time...)

    Edit:: I think there is an "ignore()" function that might also be easier than reading it in twice - though I wouldn't trust it for a prod program - in case there is more than one char in the buffer...
    Last edited by sicarie; Aug 24 '07, 02:24 PM. Reason: I think...

    Comment

    • shendraeg
      New Member
      • Aug 2007
      • 4

      #3
      I couldn't remember how to flush the buffer, but that does seem to be what's going on here. See, I knew that it was something stupid... Thanks

      Comment

      • sicarie
        Recognized Expert Specialist
        • Nov 2006
        • 4677

        #4
        Originally posted by shendraeg
        I couldn't remember how to flush the buffer, but that does seem to be what's going on here. See, I knew that it was something stupid... Thanks
        It's not stupid at all, just something that you learn as you encounter it, which depend on how you use different inputs in your programs.

        Anyway, I like cplusplus.com as a reference - that's the fflush page on a search for stdin.

        Comment

        • ilikepython
          Recognized Expert Contributor
          • Feb 2007
          • 844

          #5
          Originally posted by shendraeg
          I'm trying to use fstreams to format/encode a text file in a way that is useful for another program of mine, and I came upon a little snag...
          The input file is formated as such:

          int (line count)
          string (line)
          string (line)
          string (line)
          etc...

          obviously, I start by reading in the integer and then proceed to use getline to try and get the strings, but the problem that I'm having is that getline won't pull the strings, and thus I'm not able to format them:

          Code:
          	int line_count(0);
          	string s;
          
          	int word_count(0);
          
          	in >> line_count;
          	out << line_count;
          
          	for(int i=0; i<line_count; ++i)
          	{
                                         ...
          		getline(in, s);
                                         ...
                          }
          in and out are the ifstream and ofstream, respectively. The string seems to come in as an empty string. Both outputting it and attempting to format it reveal this. It's quite likely that I'm missing/forgetting something stupidly simple, but it's been awhile since I've written anything with straight c++ code. Any insight would be appreciated.
          I'm not sure what the problem is, but you don't need a line number:
          [code=cpp]
          while (!words.eof())
          {
          getline(in, s);
          ... do whatever with s ...
          }
          [/code]

          Edit: Wow, 3 posts in front of me, oops.

          Comment

          • shendraeg
            New Member
            • Aug 2007
            • 4

            #6
            Originally posted by sicarie
            It's not stupid at all, just something that you learn as you encounter it, which depend on how you use different inputs in your programs.

            Anyway, I like cplusplus.com as a reference - that's the fflush page on a search for stdin.
            I'll go check that out. Thanks again for your help

            Comment

            • sicarie
              Recognized Expert Specialist
              • Nov 2006
              • 4677

              #7
              No problem - I'm not even sure if it's right, it's just a common issue. Hopefully it works, if not, post again and we'll figure it out!

              Comment

              • shendraeg
                New Member
                • Aug 2007
                • 4

                #8
                Originally posted by sicarie
                No problem - I'm not even sure if it's right, it's just a common issue. Hopefully it works, if not, post again and we'll figure it out!
                That's definitely what the problem was. While a second getline crashes my program, it looks like that's due to errors in my formatting functions, which I couldn't test properly before. The second getline is definitely getting the input string now, so I should be good to go (at least after I debug my formatting, which should be simple enough).

                Comment

                Working...