| | |
Counting lines in a text file and further operations
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Apr 2007
Posts: 5
Reputation:
Solved Threads: 0
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)
string fileName; cin >> fileName; ifstream inData; inData.open( fileName.c_str() ); if ( !inData ) return 0; size = 0; string line; while ( !inData.eof() ) { getline(inData, line); size++; }
that's because the loop is wrong. You don't need to use eof() at all
C++ Syntax (Toggle Plain Text)
while( getline(inData, line) ) { size++; // do other stuff here }
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.
•
•
Join Date: Apr 2007
Posts: 5
Reputation:
Solved Threads: 0
Did some reading, I got it working I think. I just had to add:
to clear the stream and read from the beginning of the file again.
Thanks for the help.
C++ Syntax (Toggle Plain Text)
inData.clear(); inData.close(); inData.open( fileName.c_str() );
to clear the stream and read from the beginning of the file again.
Thanks for the help.
•
•
Join Date: Dec 2006
Posts: 1,089
Reputation:
Solved Threads: 164
you do not have to close and then reopen the file;
> 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:
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)
#include <iostream> #include <fstream> #include <string> #include <vector> using namespace std ; int main() { ifstream file( __FILE__ ) ; string line ; vector<string> lines ; while( getline( file, line ) ) lines.push_back( line ) ; cout << "#lines: " << lines.size() << '\n' ; }
Last edited by vijayan121; Aug 16th, 2007 at 2:09 pm.
![]() |
Similar Threads
- Reading through lines of text file sequencially (Shell Scripting)
- how to remove a number of lines from a text file ? (Python)
- Replace text in a file (Shell Scripting)
- # of lines in a text file (Java)
- how do i read the last line of a text file? (Python)
- help on using StringTokenizer to read and compute multiple lines from text (Java)
- Text File Input and manipulation (C++)
- 10 line text file (Java)
Other Threads in the C++ Forum
- Previous Thread: need help to compare a char read from a text file
- Next Thread: Tic-Tac-DOH!!
| Thread Tools | Search this Thread |
api array based beginner binary c++ c/c++ calculator char char* class classes code compile compiler console conversion count delete deploy desktop directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory news numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return rpg sorting string strings struct temperature template templates test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






