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:

#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;
}

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?

You can use strtok function using string.h
It helps u to get the string tokens based on the delimiter u give.

You know there is also www.google.com where you can search for words like "C++ split line"
or "C++ tokens"...
Or use RWCTokenizer..

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
    }
}

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
    }
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.