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?

Dani AI

Generated

Two separate issues make your loop never stop at newlines: (1) x is uninitialized when you first test it, which is undefined behavior, and (2) formatted extraction with operator>> on char skips whitespace by default, so it never returns '\n'. To see every character (including newlines), use unformatted input like istream::get, or turn off skipping of whitespace. See istream::get and std::noskipws.

Character-by-character, keeping track of line boundaries:

std::ifstream in("s1.txt");
char ch;
std::size_t line_no = 1;
while (in.get(ch)) {
    if (ch == '\r') continue;      // handle CRLF on Windows
    if (ch == '\n') { ++line_no; continue; }
    // analyze ch on line_no
}

If you prefer line-by-line, std::getline reads a full line (without the trailing newline). You can then split on commas and inspect characters, skipping spaces safely:

std::ifstream in("s1.txt");
std::string line;
for (std::size_t line_no = 1; std::getline(in, line); ++line_no) {
    std::istringstream ss(line);
    std::string field;
    while (std::getline(ss, field, ',')) {
        for (unsigned char c : field) {
            if (std::isspace(c)) continue;  // cast to unsigned char for ctype
            // analyze c from this field on line_no
        }
    }
}

Avoid strtok here; it requires mutable C strings and is not idiomatic C++. Refer to std::getline and std::isspace for details.

Recommended Answers

All 4 Replies

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.