| | |
Reading a text file character by character and line by line
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Apr 2007
Posts: 12
Reputation:
Solved Threads: 0
I have a s1.txt like this :
3,R,G,T
1,E
9,T,U,Y,O
Now I want to read each character of a each line ...do some analysis and then read each character of the next line ...do some analysis ...so on till EOF.
How do I go abt this?
Well ...I jus wrote a simple program to read a file as below:
Doing the above ...I get the entire file as output ... why is'nt it stopping execution as soon as it hits a newline as I mentioned in the first while loop? After I get the first line ...how do I parse through it to analyse each character in the line?
3,R,G,T
1,E
9,T,U,Y,O
Now I want to read each character of a each line ...do some analysis and then read each character of the next line ...do some analysis ...so on till EOF.
How do I go abt this?
Well ...I jus wrote a simple program to read a file as below:
c Syntax (Toggle Plain Text)
#include <iostream> #include <iomanip> #include <fstream> using namespace std; int main() { //char sum = 0; char x; ifstream inFile("s1.txt"); if (!inFile) { cout << "Unable to open file"; exit(1); } while (x != '\n') { while (inFile >> x) { cout<<"x = "<<x<<endl; } } inFile.close(); getchar(); return 0; }
Last edited by WaltP; Apr 12th, 2007 at 3:34 pm. Reason: Added CODE tags -- you actually typed right over what they are when you entered this post...
You know there is also www.google.com where you can search for words like "C++ split line"
or "C++ tokens"...
Or use RWCTokenizer..
or "C++ tokens"...
Or use RWCTokenizer..
•
•
Join Date: Dec 2006
Posts: 1,089
Reputation:
Solved Threads: 164
a. read one line
b. strip off unwanted chars (non-printable/whitespace/,)
c. analyze the remaining chars.
repeat a,b,c till end of file.
b. strip off unwanted chars (non-printable/whitespace/,)
c. analyze the remaining chars.
repeat a,b,c till end of file.
C++ Syntax (Toggle Plain Text)
struct is_2b_thrown_away { bool operator() ( char ch ) const { return !isprint(ch) || isspace(ch) || (ch==',') ; } }; void process_file( const char* file_name ) { ifstream file(file_name) ; string line ; while( getline( file, line ) ) { vector<char> chars ; remove_copy_if( line.begin(), line.end(), back_inserter(chars), is_2b_thrown_away() ) ; // analyse characters in vector chars } }
Mostly what vijayan121 suggests
a. read one line
b. analyze each character
c. do what you need with each character
repeat a,b,c till end of file.
a. read one line
b. analyze each character
c. do what you need with each character
repeat a,b,c till end of file.
C++ Syntax (Toggle Plain Text)
string line ; while( getline( file, line ) ) { for (i=0; i < line.length(); i++) { if (line[i] ...) // look at each character and process it accordingly } }
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
![]() |
Similar Threads
- C++ Reading from a text file (C++)
- Reading a text file (C++)
- Reading in a text file string is not complete (Pascal and Delphi)
- how do i read the last line of a text file? (Python)
Other Threads in the C++ Forum
- Previous Thread: Working with classes
- Next Thread: Borland C++
| Thread Tools | Search this Thread |
api array based binary c++ c/c++ calculator char char* class classes code coding compile console conversion count database delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory multiple news number numbertoword output parameter pointer problem program programming project 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 wordfrequency wxwidgets






