Counting lines in a text file and further operations

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Apr 2007
Posts: 5
Reputation: roflol is an unknown quantity at this point 
Solved Threads: 0
roflol roflol is offline Offline
Newbie Poster

Counting lines in a text file and further operations

 
0
  #1
Aug 16th, 2007
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).


  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. }
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,348
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1462
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Counting lines in a text file and further operations

 
0
  #2
Aug 16th, 2007
that's because the loop is wrong. You don't need to use eof() at all
  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.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Apr 2007
Posts: 5
Reputation: roflol is an unknown quantity at this point 
Solved Threads: 0
roflol roflol is offline Offline
Newbie Poster

Re: Counting lines in a text file and further operations

 
0
  #3
Aug 16th, 2007
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.
Reply With Quote Quick reply to this message  
Join Date: Apr 2007
Posts: 5
Reputation: roflol is an unknown quantity at this point 
Solved Threads: 0
roflol roflol is offline Offline
Newbie Poster

Re: Counting lines in a text file and further operations

 
0
  #4
Aug 16th, 2007
Did some reading, I got it working I think. I just had to add:

  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.
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 1,089
Reputation: vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all 
Solved Threads: 164
vijayan121 vijayan121 is offline Offline
Veteran Poster

Re: Counting lines in a text file and further operations

 
1
  #5
Aug 16th, 2007
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:
  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.
Reply With Quote Quick reply to this message  
Join Date: Nov 2009
Posts: 1
Reputation: rel0aded911 is an unknown quantity at this point 
Solved Threads: 0
rel0aded911 rel0aded911 is offline Offline
Newbie Poster
 
0
  #6
2 Days Ago
can someone write this code to c ? cause i don't know c++. thanks
Reply With Quote Quick reply to this message  
Reply

Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC