Hello everybody,
This is my second query in this post. Firstly thankx to Banfa, for helping me solve my first query.
Here is the code which I have written.
Please dont get scared with the size of the code. I'm trying to read a file called "eugene.ct.syn" (given below as attached file). Now I want to read the entire file from starting till end. When I run this code on Visual C++ Editor, the program doesnt read the entire file, but I get the message "File loaded completely" written in main. Finally the program throws an exception at the end. I want to read the entire file. If you open the file with Microsoft word you can see some text contents in it. My target is to read the file till "THE PROGRAM HAS NOW FINISHED THE PROBLEM." (which you can see in the file(eugene.ct. syn) given below as attchment after you open it). I'm attaching the program too for you easiness.
Also I'm not able to guess why this program throws an exception.
Thankx in advance,
Rajeev
This is my second query in this post. Firstly thankx to Banfa, for helping me solve my first query.
Here is the code which I have written.
Code:
#include<iostream>
#include<fstream>
using namespace std;
class myClass
{
public:
myClass(){}
friend std::istream& operator>>(std::istream& i, myClass& ct);
std::istream& extractor(std::istream& i);
};
std::istream& myClass::extractor(std::istream& i)
{
char line[256];
char keyword[64];
char origKeyword[64];
int p;
char cp;
bool finished = false;
bool endct = false;
while( i && !finished )
{
if( (p = i.peek()) == ';' || p == '#' )
{
// read the comment line and discard it
i.getline( line, 255 );
continue;
}
// check for eof or error
if( !i )
continue;
while( i && isspace( p = i.peek() ) )
{
i.get(cp);
}
// check for error or eof
if( !i )
continue;
if( !endct )
{
// check for a digit, and if we don't get one it means we
// are done with tablecounting
if( ( p = i.peek() ) != EOF && isdigit( p ) )
{
i.getline(line, sizeof(line));
continue;
}
else
{
// this is the end of the connection table proper
endct = true;
}
}
if( ( p = i.peek() ) != EOF && isalpha( p ) )
{
// read the keyword, and deal with the input
i >> origKeyword;
// make a copy of the keyword, in case we need to put it back in the stream
strcpy( keyword, origKeyword );
// change to lower case
for( char *lp = keyword; *lp; ++lp )
{
*lp = tolower( *lp );
}
if( i )
{
if( !strncmp( keyword, "cis", 3 ) )
{
i.getline(line, sizeof(line));
}
else if( !strncmp( keyword, "trans", 5 ) )
{
i.getline(line, sizeof(line));
}
else if( !strncmp( keyword, "entgegen", 8 ) )
{
i.getline(line, sizeof(line));
}
else if( !strncmp( keyword, "zusammen", 8 ) )
{
i.getline(line, sizeof(line));
}
else if( !strncmp( keyword, "chiral", 6 ) )
{
i.getline(line, sizeof(line));
}
else if( !strncmp( keyword, "donttouch", 9 ) )
{
i.getline(line, sizeof(line));
}
else if( !strncmp( keyword, "startwith", 9 ) )
{
i.getline(line, sizeof(line));
}
else if( !strncmp( keyword, "keybond", 7 ) )
{
i.getline(line, sizeof(line));
}
else if( !strncmp( keyword, "planetrans", 10 ) )
{
i.getline(line, sizeof(line));
}
else if( !strncmp( keyword, "planecis", 8 ) )
{
i.getline(line, sizeof(line));
}
else if( !strncmp( keyword, "available", 9 ) )
{
i.getline(line, sizeof(line));
}
else
{
int len = strlen( origKeyword );
for(char c = origKeyword[len-1]; len>0; --len)
{
c = origKeyword[len - 1];
i.putback(c);
}
finished = true;
}
}//if i
continue;
}
if( EOF == i.peek() )
break;
}//while
return i;
}
std::istream& operator>>(std::istream& i, myClass& ct)
{
return ct.extractor( i );
}
bool loadFile( std::istream &i )
{
if(i)
{
int ii = 1;
char line[256];
int p;
char cp;
bool finished = false;
int routesw = 2; // routesw == 2 means the beginning of the file
int ctsw = 0;
int infosw = 0;
myClass* ct = NULL;
while( i && !finished )
{
if( (p = i.peek()) == ';' || p == '#' )
{
// read the comment line and discard it
i.getline( line, sizeof() );
continue;
}
// check for eof or error
if( !i )
continue;
// If we haven't reached EOF, read the line, and parse.
// Otherwise break the loop and finish.
// The getline() function reads to the '\n' (or sizeof(line) - 1),
// discards it, and appends a '\0' to the line.
if( ( p = i.peek() ) != EOF )
{
i.getline( line, sizeof(line) );
cout<<line<<endl;
if(ii == 4017)
{
cout<<ii<<endl;
}
ii++;
}
else
break;
if(i)
{
if( strstr(line,"CONNECTION TABLE") )
{
// The connection table starts on the next line,
// and so we read it here.
// skip blank space at the beginning of the line
while( i && isspace( p = i.peek() ) )
{
i.get(cp);
}
// check for error or eof
if( !i )
continue;
// Check for a digit, meaning we are about to read a
// ConnectionTable. Only allocate a new ct if we
// are not at EOF and the next line begins with a digit.
if( ( p = i.peek() ) != EOF && isdigit( p ) )
{
// Allocate a new connection table
ct = new myClass;
i >> *ct;
}
i.clear();
// if we are at EOF then break
if( EOF == i.peek() )
{
finished = true;
break;
}
}
} //if(i)
// check whether we are at the end of the file
if( EOF == i.peek() )
break;
}//while
return true;
}//if(i)
return false;
}
int main(int argc, char* argv[])
{
ifstream t("eugene.ct.syn", std::ios::in );
if(t)
{
t.seekg( 0, std::ios::beg );
if(loadFile( t ))
{
cout<<"File loaded completely"<<endl;
}
else
cout<<"File failed to load"<<endl;
}
t.close();
return 0;
}
Also I'm not able to guess why this program throws an exception.
Thankx in advance,
Rajeev
Comment