Hi all,

I have little problem when reading a file char by char and when i tried to make a check at the end of file. I am making a check through '\n' but its not working. Please have a look in the following Program and let me know yours suggestion. The following Program removes comments from a C++ file. The logic of this program is taken from state machine diagram.

#include<iostream>
#include<fstream>

using namespace std;

class stateMachine{
     public:
        stateMachine();
        ~stateMachine();      
      
        void process_input();
      
      private:   
           enum StateNames { A = 1, B, C, D, E};
           int currState;
           ifstream orgnl;
           ofstream bakup;
};
      
stateMachine::stateMachine(){
     orgnl.open("exp1.cpp");
     bakup.open("exp2.cpp");  
     
   currState = A;
}

stateMachine::~stateMachine(){
     orgnl.close();
     bakup.close();
}

void stateMachine::process_input(){
     char ch;
     while(!orgnl.eof()){
        orgnl >> ch;
        switch(currState){          
           case A: {
                if (ch == '/' || ch == '*')
                   currState = B;
                break;
           }
           
           case B: {
                if(ch == '/')
                   currState = C;
                 
                else if(ch == '*')
                   currState = D;
                
                else
                   currState = A;                  
                break;
           }
           
           case C: {
                cout<<"Char is : "<<ch<<"\tand Current state is : "<<currState<<endl;
                if(ch == '\n')
                   currState = A;                  
                break;
           }
           
           case D: {
                if(ch == '*')
                  currState = E;
                break;
           }
           
           case E: {
                if(ch == '/')
                  currState = A;
                
                else
                  currState = D;
                break;
           }
        }   
        if(currState == A)
            bakup << ch;  
     } 
}

int main(){
    stateMachine sm;
    sm.process_input();
    
    system("pause");
    return 0;
}

exp1.cpp file contais:

#include<iostream>

using namespace std;

int main(){
    char ch;      //char
    /*inputting char and int*/
     return 0;
}

The output of above program is (i have putted cout in state C):

Char is : c and Current state is : 3
Char is : h and Current state is : 3
Char is : a and Current state is : 3
Char is : r and Current state is : 3
Char is : / and Current state is : 3
Char is : * and Current state is : 3
Char is : i and Current state is : 3
Char is : n and Current state is : 3
Char is : p and Current state is : 3
Char is : u and Current state is : 3
Char is : t and Current state is : 3
Char is : t and Current state is : 3
Char is : i and Current state is : 3
Char is : n and Current state is : 3
Char is : g and Current state is : 3
Char is : c and Current state is : 3
Char is : h and Current state is : 3
Char is : a and Current state is : 3
Char is : r and Current state is : 3
Char is : a and Current state is : 3
Char is : n and Current state is : 3
Char is : d and Current state is : 3
Char is : i and Current state is : 3
Char is : n and Current state is : 3
Char is : t and Current state is : 3
Char is : * and Current state is : 3
Char is : / and Current state is : 3
Char is : r and Current state is : 3
Char is : e and Current state is : 3
Char is : t and Current state is : 3
Char is : u and Current state is : 3
Char is : r and Current state is : 3
Char is : n and Current state is : 3
Char is : 0 and Current state is : 3
Char is : ; and Current state is : 3
Char is : } and Current state is : 3
Char is : } and Current state is : 3
Press any key to continue . . .

Which indicates program is stuck on state 'C'. Why Program is not getting identify '\n' as eol. What could be the right solution to make this program work.

sorry but i didnt get you. A, B, C ...are states and defined in here as enum type.


My question is: Why compiler is not identifying end of line at case 'C' of switch block. I am using Dev C++.

I can see what they are.

But I'd rather see words like IN_COMMENT or POSSIBLE_COMMENT as the names of states, rather than what you've got.

With decent names, it might be plainly obvious that you can't (for example) get from IN_COMMENT to END_OF_LINE in one character.

A B C do not do that.

Comments
Makes me happy seeing other people using good variable names :)

I agree with you sir. Name here are bit lack in meaning. Actual problem is not visible in this code.

Please consider following code:

#include<iostream>
#include<fstream>

using namespace std;

int main(){
    ifstream fp1("xyz.txt");
   char ch;
   for(;ch!='\n' && !(fp1.eof());fp1 >> ch)
       cout<<ch;
    system("pause");
    return 0;
}

xyz.txt is multi-line txt file and i'm willing to print its first line. This code is not looking right in execution. My question is what is the problem. i hope, i'm hitting on right problem which i'm facing in above code.

It seems to me that reads up to the first newline, then exits.
Is that what you want?

while ( fp1 >> ch ) {
}

should read the whole file.

If you've got something to do at a newline, then

while ( fp1 >> ch ) {
  if ( ch == '\n' ) {
    // woo hoo, newline goodness goes here
  }
}

Well i think that you should use getline(); and then get the input onto a stringstream or a string and then follow onto the next thing. with the string or stringstream itself.

Other than that i dont know a way of finding the end of the line. Because i am new to files .


Hope this helps

Edit : Seems that i am wrong. Ignore this post.

Well sir thats what i'm saying what is my problem. ch read by (fp1 >> ch) is not getting '\n' at the end of ine (or fp1 >> ch doesn't support '\n'....that i dont know)

lets try a simple problem.

#include<iostream>
#include<fstream>

using namespace std;

int main(){
    ifstream fp1("xyz.txt");
    char ch;
    while ( fp1 >> ch && !(fp1.eof())) {
    cout<<ch;
    if ( ch == '\n' ) 
      break;
    }
    cout<<endl;
    system("pause");
    return 0;
}

The above one is your program. xyz.txt file looks like as :

I love sunshine.
I love stars.

now on execution it shows

Ilovesunshine.ilovestars
Press any key to continue . . .

If output is different in your compiler please let me know. if not then whats the problem. Why only first line not getting printed.

It should be noted that using ifstream with >> operator defaults to skipping whitespace characters, which '\n' is also included in. if you want to proccess the'\n' in a special manner then you should use the following loop condition while(fp1 >> noskipws >> ch) Not that you do not need to check eof, the >>operator returns false if it cannot read anymore chars...end of file. Also eof() can be trigger by certain control characters and hence should never be used to check for the end of the file

Also if you are after one line, I would recommend using getline(). Not only is it simpler, but it also faster. Reading multiple bytes from a file at once is faster than reading byte by byte

Chris

Comments
He saved my life

sir freaky, you are a life saver. After hours of googling i am surprised why i didn't get 'noskipws' concept. May be i'm searching with wrong keywords.Thanks for your help. :icon_smile:

This question has already been answered. Start a new discussion instead.