Thread: Print to file issue

  1. #1
    Registered User
    Join Date
    Aug 2006
    Posts
    17

    Print to file issue

    After going through this numerous times and making changes, I can't seem to figure out why this is not printing to the ofstream file.

    Code:
     
    
       #include <iostream>
       #include <fstream>
       #include <string>
       #include <ctype.h>
       #define TRUE 1
       #define FALSE 0
       using namespace std;
    
    
    
          typedef enum
          /* book keeping tokens */
          { ENDFILE, ERROR,
          /* reserved words */
          IF, THEN, ELSE, END, REPEAT, UNTIL, READ, WRITE,
          /* multicharacter tokens */
          ID, NUM,
          /* special symbols */
           ASSIGN, EQ, LT, PLUS, MINUS, TIMES, OVER, LPAREN, RPAREN, SEMI,
           /* Output */
           OUTPUT
          } TokenType;
    
    
    
    
     /* print function */
    void printToken( TokenType token, char arg[], ofstream &outfile)
    {
    
    switch ( token )
    {
      case IF:      cout << "reserved word IF: " << arg << endl; break;
      case THEN:    cout << "reserved word THEN: " << arg << endl; break;
      case ELSE:    cout << "reserved word ELSE: " << arg << endl; break;
      case END:     cout << "reserved word END: " << arg << endl; break;
      case REPEAT:  cout << "reserved word REPEAT: " << arg << endl; break;
      case UNTIL:   cout << "reserved word UNTIL: " << arg << endl; break;
      case READ:    cout << "reserved word READ: " << arg << endl; break;
      case WRITE:   cout << "reserved word WRITE: " << arg << endl; break;
      case ASSIGN:  cout << ":=\n"; break;
      case LT:      cout << "<\n"; break;
      case EQ:      cout << "=\n"; break;
      case LPAREN:  cout << "(\n"; break;
      case RPAREN:  cout << ")\n"; break;
      case SEMI:    cout << ";\n"; break;
      case PLUS:    cout << "+\n"; break;
      case MINUS:   cout << "-\n"; break;
      case TIMES:   cout << "*\n"; break;
      case OVER:    cout << "/\n"; break;
      case ENDFILE: cout << "EOF\n"; break;
      case NUM:     cout << "NUM, val= " << arg << endl; break;
      case ID:      cout << "ID, name= " << arg << endl; break;
      case OUTPUT:  cout << arg << endl; break;
      case ERROR:   cout << "ERROR: \n"; break;
    
      default:      cout << "Unknown token: \n" << token;
    
    
    }
    }
    
    
    
    
    int main ( void )
    {
    
       char junk;
       char ch;
       char read[ 80 ];
       char write[ 80 ];
       char tokString[ 256 ];
    
       int lineno = 0;
       int tokenIndex = 0;
    
      cout<<"\nEnter source file name: " << endl;
      cin >> read;
    
      ifstream rfile ( read );
      if (! rfile.is_open ( ) )
      {
        cout << "Error opening file";
        exit ( 1 );
      }
    
      cout<<"\nEnter destination file name: " << endl;
      cin >> write;
    
      ofstream wfile ( write );
      if (! wfile.is_open ( ) )
      {
        cout << "Error opening file";
        exit ( 1 );
      }
    
    
    
       /* Preform scan*/
    
       TokenType currentToken;
       short tokenFound;
       ch = rfile.get();
       while( ! rfile.eof() )
       {
         tokenFound = TRUE;
         /* digit check */
         while ( isspace ( ch ) && ! rfile.eof () )
            ch = rfile.get();
    
    		if( isdigit ( ch ) )
    			{
    				tokenIndex = 0;
    				while ( isdigit ( ch ) )
    				{
    				tokString [ tokenIndex ++ ] = ch;
    				ch = rfile . get ( ) ;
    				}
    				tokString  [ tokenIndex ] = 0;
    				currentToken = NUM;
    			} //END DIGIT
        
         else if ( isalpha ( ch ) )
    		{
    
            tokenIndex = 0;
            while( isalpha ( ch ) )
    			{
              tokString[ tokenIndex ++ ] = ch;
              ch = rfile.get();
    			}
            tokString [ tokenIndex ] = 0; //add null to end of string
    
            if(strcmp(tokString, "IF")==0  || strcmp(tokString,"if") == 0 )
    			{
               currentToken = IF;
    			}
            else if(strcmp(tokString, "THEN")==0  || strcmp(tokString,"then") == 0 )
    			{
               currentToken = THEN;
    			}
            else if(strcmp(tokString, "ELSE")==0  || strcmp(tokString,"else") == 0 )
    			{
               currentToken = ELSE;
    			}
            else if(strcmp(tokString, "END")==0  || strcmp(tokString,"end") == 0 )
    			{
               currentToken = END;
    			}
            else if(strcmp(tokString, "REPEAT")==0  || strcmp(tokString,"repeat") == 0)
    			{
               currentToken = REPEAT;
    			}
            else if(strcmp(tokString, "UNTIL")==0  || strcmp(tokString,"until")== 0)
    			{
               currentToken = UNTIL;
    			}
            else if(strcmp(tokString, "READ")==0  || strcmp(tokString,"read")== 0)
    			{
              currentToken = READ;
    			}
            else if(strcmp(tokString, "WRITE")==0  || strcmp(tokString,"write") == 0)
    			{
               currentToken = WRITE; /* This is were it gets hung up at  */
    			}
            else
    			{
                currentToken = ID;
    			}
    		}
        else if( ch == ':' )
    		{
    
           ch = rfile.peek();
           if ( ch == '=' )
    			{
             rfile.get();
             ch = rfile.get();
             currentToken = ASSIGN;
    			}
    
    		}
    
        else if( ch == '{' ) //comment
        {
           ch = rfile.get();
           tokenIndex = 0;
           while(ch != '}' && ! rfile.eof () )
           {
             tokString[ tokenIndex ++ ] = ch;
             ch = rfile.get();
           }
           tokString [ tokenIndex ] = 0;
           ch = rfile.get(); // get }
           currentToken = OUTPUT;
        }
    
        else
        {
           switch ( ch )
           {
    
               case '\n':
                  lineno++;
                 break;
               case EOF:
                 currentToken = ENDFILE;
                 break;
               case '=':
                 currentToken = EQ;
                 break;
               case '<':
                 currentToken = LT;
                 break;
               case '+':
                 currentToken = PLUS;
                 break;
               case '-':
                 currentToken = MINUS;
                 break;
               case '*':
                 currentToken = TIMES;
                 break;
               case '/':
                 currentToken = OVER;
                 break;
               case '(':
                 currentToken = LPAREN;
                 break;
               case ')':
                 currentToken = RPAREN;
                 break;
               case ';':
                 currentToken = SEMI;
                 break;
               default:
               {
                  cout << "bad token: " << ch << endl;
                  tokenFound = FALSE;
               };
           }
           ch = rfile.get();
    
        }
        if ( tokenFound )
           printToken( currentToken, tokString, wfile);
        else
          tokenFound = TRUE;
     }
     printToken ( ENDFILE , "eof" , wfile ); //loop broke
    
    
    /* end scan */
    
         /* pause output */
          cout << "\nScan is done hit a char and Hit Enter" << endl;
          cin >> junk;
    
       /* close files */
    
      rfile.close();
      wfile.close();
    
    
      return 0;
    }
    Any ideas are welcomed,
    thanks.

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    > case IF: cout << "reserved word IF: " << arg << endl; break;
    You're writing to cout. Change it to outfile:
    Code:
      case IF:      outfile << "reserved word IF: " << arg << endl; break;
    And if you want to write to both, you could call printToken twice, first passing it wfile as the ofstream and second passing it cout. You may have to change it to an ostream though, I'm not sure.

  3. #3
    Registered User
    Join Date
    Aug 2006
    Posts
    17
    The devil's in the details, should have seen that! thanks a bunch..

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File transfer- the file sometimes not full transferred
    By shu_fei86 in forum C# Programming
    Replies: 13
    Last Post: 03-13-2009, 12:44 PM
  2. gcc link external library
    By spank in forum C Programming
    Replies: 6
    Last Post: 08-08-2007, 03:44 PM
  3. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. What kind of programs should I start writing?
    By Macabre in forum C++ Programming
    Replies: 23
    Last Post: 04-12-2003, 08:13 PM