User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 391,700 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,172 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser:
Views: 5483 | Replies: 4
Reply
Join Date: Apr 2007
Posts: 12
Reputation: B.Y is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
B.Y B.Y is offline Offline
Newbie Poster

Solution Reading a text file character by character and line by line

  #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 2:34 pm. Reason: Added CODE tags -- you actually typed right over what they are when you entered this post...
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Aug 2006
Posts: 56
Reputation: rati is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
rati rati is offline Offline
Junior Poster in Training

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

  #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  
Join Date: Feb 2007
Location: Bangalore, India
Posts: 535
Reputation: thekashyap will become famous soon enough thekashyap will become famous soon enough 
Rep Power: 4
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

  #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  
Join Date: Dec 2006
Location: india
Posts: 1,045
Reputation: vijayan121 is a glorious beacon of light vijayan121 is a glorious beacon of light vijayan121 is a glorious beacon of light vijayan121 is a glorious beacon of light vijayan121 is a glorious beacon of light vijayan121 is a glorious beacon of light 
Rep Power: 9
Solved Threads: 157
vijayan121 vijayan121 is offline Offline
Veteran Poster

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

  #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.

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
    }
}
Reply With Quote  
Join Date: May 2006
Posts: 2,698
Reputation: WaltP is a splendid one to behold WaltP is a splendid one to behold WaltP is a splendid one to behold WaltP is a splendid one to behold WaltP is a splendid one to behold WaltP is a splendid one to behold 
Rep Power: 14
Solved Threads: 219
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Maven

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

  #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.

string line ;
while( getline( file, line ) )
{
    for (i=0; i < line.length(); i++)
    {
         if (line[i] ...)    // look at each character and process it accordingly
    }
}
Age is unimportant -- except in cheese
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

DaniWeb C++ Marketplace
Thread Tools Display Modes

Similar Threads
Other Threads in the C++ Forum

All times are GMT -4. The time now is 2:41 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC