Can not open file

LLeweLLyn Reese llewelly@lifesupport.shutdown.com
Sun Mar 9 01:07:00 GMT 2003


"Karl Gangle" <kgangle72@attbi.com> writes:

> Hi,
> 
> I am porting a program from a previous version of Linux.  In the older
> version of Linux the program works fine.  But, in the new version the
> program has several problems.  I am having trouble opening a file with the
> following program module.  See the section marked with 11111's.  What needs
> to be done to open the file?

Please read:
http://gcc.gnu.org/onlinedocs/libstdc++/faq/index.html#4_4_iostreamclear

and add a call to infile.clear() after the infile.close() in
    SourceFile::FileSize()  .

> 
> Thank you.
> 
> Karl Gangle
> 
> 
> Source file.h:
> 
> #ifndef SourceFile_Header
> #define SourceFile_Header
> 
> #include <fstream>
> #include <string>
> #include <iostream>
> 
> class SourceFile {
> 
>   private:
>     ifstream infile;
>     int filesize;
>     int bufferpostn;
>     char *buffer;
>     char filename[ 50 ];
> 
>   public:
>     SourceFile( void ) { filesize=0; bufferpostn=0; }
>     ~SourceFile( void ) { delete buffer; }
>     void FileSize( char * );
>     void BufferFile( char * );
>     virtual char *gettoken( void );
>     virtual char *peektoken( void );
>     virtual void OpenFile( char * );
>     virtual void CloseFile( void );
>     virtual bool eosf( void );
>     virtual bool eosl( void );
>     bool eob( void );
>     char get( void );
>     char peek( void );
>     void seekp( int );
>     int getbufferpostn( void );
> };
> 
> #endif
> 
> Source File.cpp:
> 
> // gettoken and peektoken
> 
> #include "SourceFile1.h"
> #include <string>
> 
> using namespace std;
> 
> void SourceFile::OpenFile( char *fname ) {
>   strcpy( filename, fname );
> 
>   cout << "filename: " << fname << endl << flush;
>   FileSize( filename );
>   BufferFile( filename );
> 
> }
> 
> void SourceFile::BufferFile( char *fname ) {
> 
> //11111111111111111111111111111111111111111111111111111111111
> //   the following line does not open the file fname.
>     infile.open( fname );
> //11111111111111111111111111111111111111111111111111111111111
> 
>     buffer = new char[ filesize ];
> 
>     for ( bufferpostn=0; bufferpostn < filesize; bufferpostn++ )
>       buffer[ bufferpostn ] = infile.get();
> 
>     infile.close();
> 
>     cout << "filesize: " << filesize << endl << flush;
>     bufferpostn = 0;
> 
> }
> 
> void SourceFile::CloseFile( void ) {
>   infile.close();
>   delete[] buffer;
> }
> 
> void SourceFile::FileSize( char *fname ) {
> 
>   cout << "filesize-filename: " << fname << endl << flush;
>   infile.open( fname );
> 
>   for ( filesize=0; !infile.eof(); filesize++ )
>     infile.get();
> 
>   filesize--;  // less the -1 for end of file.
> 
>   infile.close();
> 
> }
> 
> char SourceFile::get( void ) {
> 
>   if ( bufferpostn < 0 )
>     return buffer[ 0 ];
>   else
>     if ( bufferpostn >= filesize )
>       return buffer[ filesize-1 ];
>     else
>       return buffer[ bufferpostn++ ];
> }
> 
> char SourceFile::peek( void ) {
> 
>   if ( bufferpostn < 0 )
>     return buffer[ 0 ];
>   else
>     if ( bufferpostn >= filesize )
>       return buffer[ filesize-1 ];
>     else
>       return buffer[ bufferpostn ];
> 
> }
> 
> void SourceFile::seekp( int postn ) {
> 
>   if ( postn < 0 )
>     bufferpostn=0;
>   else
>     if ( postn > filesize )
>       bufferpostn = filesize;
>     else
>       bufferpostn = postn;
> }
> 
> int SourceFile::getbufferpostn( void ) {
> 
>   return bufferpostn;
> 
> }
> 
> bool SourceFile::eob( void ) {
> 
>   if ( bufferpostn == filesize )
>     return true;
>   else
>     return false;
> }
> 
> bool SourceFile::eosf( void ) {
>   int notatend=0, i, orginalfilepostn;
>   char peektoken;
>   int filepostn;
> 
>   orginalfilepostn = getbufferpostn();
> 
>   for ( i=orginalfilepostn; i<(orginalfilepostn+10) && i<filesize; i++ ) {
> 
>     seekp( i );
>     peektoken = peek();
> 
>     if ( i != filesize ) {
> 
>        if ( isalnum( peektoken ) )
>          notatend++;
>        else {
>          if ( peektoken==',' || peektoken==';' || peektoken==':' ||
>            peektoken=='{' || peektoken=='}' || peektoken=='(' ||
>            peektoken==')' )  {
>               notatend++;
>          }
>        }
>     }
>   } // for
> 
> 
>   seekp( orginalfilepostn ); // Set buffer index back to orginal position.
> 
>   if ( notatend > 0 )
>     return false; // Not at end of file.
>   else
>     return true;
> 
> }
> 
> bool SourceFile::eosl( void ) {
>   char c;
> 
>   c=peek();
>   return (c=='\n' ? true:false);
> }
> 
> char *SourceFile::gettoken( void ) {
>   int done = 1, pdone=1, count=0;
>   string token;
>   char peektoken=0;
>   static char tokenstr[ 50 ];
> 
>     token.assign( 20, '\0' );
> 
>     if ( getbufferpostn() != filesize ) {
>       while( done ) {
>         do {
>           peektoken = peek();
>           if ( getbufferpostn() == filesize ) {
>             strcpy( tokenstr, "" );
>             return tokenstr;
>           }
>           if ( count == 0 && (peektoken == '\r' || peektoken == '\n' ||
> peektoken == ' ') )
>             get();
>           else
>             pdone = 0;
>         }
>         while ( pdone );
> 
>         if ( ( peektoken==',' || peektoken==';' || peektoken==':' ||
>            peektoken=='{' || peektoken=='}' || peektoken=='(' ||
>            peektoken==')' || peektoken==' ' || peektoken=='\n' ||
> peektoken=='.' ||
>            peektoken=='#' || peektoken=='\"' || peektoken=='*' ||
> peektoken=='&' ) ) {
>           if ( peektoken == ':' && count == 0 ) {
>             token = get();
> 
>             if ( peek() == ':' )
>               token += get();
>           }
>           else
>             if ( peektoken == ' ' )
>               get();
>            else
>              if ( peektoken != ' ' && count == 0 )
>                token = get();
> 
>           done = 0;
>         }
>         else {
>           if ( count==0 )
>             token = get();
>           else
>             token += get();
>           count++;
>         }
>       }
>     }
>     else {
>       cerr << "\nERROR: Premature end of file\n";
>       cerr.flush();
>       exit(-1);
>     }
> 
> 
>   strcpy( tokenstr, token.c_str() );
> 
>   return tokenstr;
> 
> }
> 
> char *SourceFile::peektoken( void ) {
>   int done = 1, i, count=0, pdone=1, orginalfilepostn=0;
>   char peektoken = 0;
>   string token( 20, '\0');
>   static char tokenstr[ 50 ];
> 
>     orginalfilepostn = getbufferpostn();
> 
>     if ( getbufferpostn() != filesize ) {
>       while( done ) {
>         do {
> 
>           peektoken = peek();
>           if ( getbufferpostn() == filesize ) {
>             cerr << "\nERROR: Premature end of file\n";
>             cerr.flush();
>             exit(-1);
>           }
>           if ( (peektoken=='\r' || peektoken=='\n' || peektoken==' ') &&
> count==0 )
>             get();
>           else
>             pdone = 0;
>         }
>         while ( pdone );
> 
>         if ( (peektoken==',' || peektoken==';' || peektoken==':' ||
>            peektoken=='{' || peektoken=='}' || peektoken=='(' ||
>            peektoken==')' || peektoken==' ' || peektoken=='#' ||
> peektoken=='.' ||
>            peektoken=='\"' || peektoken=='*' || peektoken=='&' ||
> peektoken=='\n' ) ) {
>           if ( peektoken == ':' && count == 0 ) {
>             token = get();
> 
>             if ( peek() == ':' ) {
>               token += get();
>             }
>           }
>           else
>             if ( peektoken != ' ' && count == 0 )
>               token = peektoken;
> 
>           done = 0;
>         }
>         else {
>           if ( count==0 )
>             token = get();
>           else
>             token += get();
>           count++;
>         }
>       } // while
>     }
> 
>     seekp( orginalfilepostn );  // set back to orginal file buffer position
> 
> 
>   strcpy( tokenstr, token.c_str() );
> 
>   return tokenstr;
> }



More information about the Gcc-help mailing list