Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C++ (http://www.go4expert.com/forums/cpp/)
-   -   File Handling : Problem Reading end of Line (http://www.go4expert.com/forums/file-handling-reading-line-t15872/)

majesticmanish 19Jan2009 22:33

File Handling : Problem Reading end of Line
 
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.
Code:

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

Code:


#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):


Code:

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.

asadullah.ansari 20Jan2009 10:14

Re: File Handling : Problem Reading end of Line
 
provide file also means exp1.cpp & exp2.cpp


All times are GMT +5.5. The time now is 11:51.