Reading a text file character by character and line by line

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

Join Date: Apr 2007
Posts: 12
Reputation: B.Y is an unknown quantity at this point 
Solved Threads: 0
B.Y B.Y is offline Offline
Newbie Poster

Reading a text file character by character and line by line

 
0
  #1
Apr 12th, 2007
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:
  1. #include <iostream>
  2. #include <iomanip>
  3. #include <fstream>
  4. using namespace std;
  5. int main() {
  6. //char sum = 0;
  7. char x;
  8. ifstream inFile("s1.txt");
  9. if (!inFile) {
  10. cout << "Unable to open file";
  11. exit(1);
  12. }
  13. while (x != '\n') {
  14. while (inFile >> x) {
  15.  
  16. cout<<"x = "<<x<<endl;
  17.  
  18. }
  19. }
  20. inFile.close();
  21.  
  22. getchar();
  23. return 0;
  24. }
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?
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...
Reply With Quote Quick reply to this message  
Join Date: Aug 2006
Posts: 77
Reputation: rati is an unknown quantity at this point 
Solved Threads: 0
rati rati is offline Offline
Junior Poster in Training

Re: Reading a text file character by character and line by line

 
0
  #2
Apr 12th, 2007
You can use strtok function using string.h
It helps u to get the string tokens based on the delimiter u give.
Reply With Quote Quick reply to this message  
Join Date: Feb 2007
Posts: 539
Reputation: thekashyap will become famous soon enough thekashyap will become famous soon enough 
Solved Threads: 50
thekashyap's Avatar
thekashyap thekashyap is offline Offline
Posting Pro

Re: Reading a text file character by character and line by line

 
0
  #3
Apr 12th, 2007
You know there is also www.google.com where you can search for words like "C++ split line"
or "C++ tokens"...
Or use RWCTokenizer..
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: Reading a text file character by character and line by line

 
0
  #4
Apr 12th, 2007
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.

  1. struct is_2b_thrown_away
  2. {
  3. bool operator() ( char ch ) const
  4. { return !isprint(ch) || isspace(ch) || (ch==',') ; }
  5. };
  6.  
  7. void process_file( const char* file_name )
  8. {
  9. ifstream file(file_name) ;
  10. string line ;
  11. while( getline( file, line ) )
  12. {
  13. vector<char> chars ;
  14. remove_copy_if( line.begin(), line.end(), back_inserter(chars),
  15. is_2b_thrown_away() ) ;
  16. // analyse characters in vector chars
  17. }
  18. }
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 3,114
Reputation: WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of 
Solved Threads: 281
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Sensei

Re: Reading a text file character by character and line by line

 
0
  #5
Apr 12th, 2007
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.

  1. string line ;
  2. while( getline( file, line ) )
  3. {
  4. for (i=0; i < line.length(); i++)
  5. {
  6. if (line[i] ...) // look at each character and process it accordingly
  7. }
  8. }
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC