944,093 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 43398
  • C++ RSS
Aug 16th, 2007
0

Counting lines in a text file and further operations

Expand Post »
So I'm trying to count the number of lines in a text file. I think I got it working, but it seems I can't do further correct processing on the text file, maybe because getline extracts the data? The operations I want to perform include inputting values read from the file into an array (This works correctly if I comment out the code for counting the lines, but the output is wrong if I include the code).


C++ Syntax (Toggle Plain Text)
  1.  
  2. string fileName;
  3. cin >> fileName;
  4. ifstream inData;
  5.  
  6. inData.open( fileName.c_str() );
  7. if ( !inData )
  8. return 0;
  9.  
  10. size = 0;
  11. string line;
  12. while ( !inData.eof() )
  13. {
  14. getline(inData, line);
  15. size++;
  16. }
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
roflol is offline Offline
5 posts
since Apr 2007
Aug 16th, 2007
0

Re: Counting lines in a text file and further operations

that's because the loop is wrong. You don't need to use eof() at all
C++ Syntax (Toggle Plain Text)
  1. while( getline(inData, line) )
  2. {
  3. size++;
  4. // do other stuff here
  5. }
Last edited by Ancient Dragon; Aug 16th, 2007 at 9:44 am.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is online now Online
21,959 posts
since Aug 2005
Aug 16th, 2007
0

Re: Counting lines in a text file and further operations

There's still a problem : /, I need the number of lines before I can correctly do the other stuff.
Last edited by roflol; Aug 16th, 2007 at 10:28 am.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
roflol is offline Offline
5 posts
since Apr 2007
Aug 16th, 2007
0

Re: Counting lines in a text file and further operations

Did some reading, I got it working I think. I just had to add:

C++ Syntax (Toggle Plain Text)
  1. inData.clear();
  2. inData.close();
  3. inData.open( fileName.c_str() );

to clear the stream and read from the beginning of the file again.

Thanks for the help.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
roflol is offline Offline
5 posts
since Apr 2007
Aug 16th, 2007
1

Re: Counting lines in a text file and further operations

you do not have to close and then reopen the file; inData.clear() ; inData.seekg(0) ; is more efficient.
> I need the number of lines before I can correctly do the other stuff.
you do not; use a vector instead of a c-style array:
C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4. #include <vector>
  5. using namespace std ;
  6.  
  7. int main()
  8. {
  9. ifstream file( __FILE__ ) ;
  10. string line ;
  11. vector<string> lines ;
  12. while( getline( file, line ) ) lines.push_back( line ) ;
  13. cout << "#lines: " << lines.size() << '\n' ;
  14. }
Last edited by vijayan121; Aug 16th, 2007 at 2:09 pm.
Reputation Points: 1159
Solved Threads: 285
Posting Virtuoso
vijayan121 is offline Offline
1,606 posts
since Dec 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: I'm having trouble with nodes?
Next Thread in C++ Forum Timeline: Can Strategies be Friend?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC