File Handling : Problem Reading end of Line

Discussion in 'C++' started by majesticmanish, Jan 19, 2009.

  1. majesticmanish

    majesticmanish New Member

    Joined:
    Dec 24, 2008
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    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;
    }[FONT=tahoma]
    [/FONT]


    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.
     
  2. asadullah.ansari

    asadullah.ansari TechCake

    Joined:
    Jan 9, 2008
    Messages:
    356
    Likes Received:
    14
    Trophy Points:
    0
    Occupation:
    Developer
    Location:
    NOIDA
    provide file also means exp1.cpp & exp2.cpp
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice