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. - #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;
-
}
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
Aug 23 '06
18 3132
I'm still waiting for any suggestions.
Thanks
Banfa 9,065
Recognized Expert Moderator Expert
My thoughts, without getting into too much detail, is that the structure of your code is wrong. You clearly can not unget so many characters so you will need to re-write the code so that this is not required for it to work.
I would not give control of reading a file to a sub-object if that sub-object is not able to detect the end of it's own data and instead ends up reading the data for the start of the next object. I would leave control in the main loop when the start of a new object can be handled correctly.
Please elaborate, I'm unable to get what you meant.
Hi,
Please help me to solve that problem.
I'm in a big trouble.
Banfa 9,065
Recognized Expert Moderator Expert
Sorry this must have slipped through the net.
What I mean is at the moment you code effective trys this -
OPEN FILE
-
-
LOOP
-
LOAD DATA
-
IF OBJECT FOUND CALL OBJECT LOAD DATA FUNCTION
-
UNTIL NO MORE DATA
-
-
CLOSE FILE
-
and OBJECT LOAD DATA FUNCTION does this -
LOOP
-
LOAD DATA
-
IF PART OF THIS OBJECT
-
STORE DATA
-
ELSE
-
PUT DATA BACK // This is not working
-
ENDIF
-
UNTIL START OF NEXT OBJECT FOUND
-
The problem is that the PUT DATA BACK section of your code is not working and this is not necessarily because the code is wrong but because the underlying system just can not do what you are asking.
You need to change this structure to something that does not rely on being able to put back the data, more like this -
OPEN FILE
-
-
LOOP
-
LOAD DATA
-
-
IF DATA START OF OBJECT
-
CREATE NEW OBJECT
-
STORE DATA IN OBJECT
-
ELSE IF DATA PART OF CURRENT OBJECT
-
STORE DATA IN OBJECT
-
ELSE
-
DISCARD DATA // or do something else with it
-
ENDIF
-
UNTIL NO MORE DATA
-
-
CLOSE FILE
-
This code structure does not try to PUT BACK DATA and therefore will not run into the same problem. I don't think it is good practice to have an object read it's own data unless it can reliably detect the end of it's own data without having to read the next object.
Thankx Banfa,
I'll start my implementation very soon. If any problem I'll let you know.
Rajeev
After a long thinking, I partially got what you are talking about.
As per my understanding, I need to create an object of class istream, assign value of original istream into this newly created one and then read the first keyword then check all the "if else" blocks then finally return the original istream in the else block which in code seems to be like this - OPEN FILE
-
-
LOOP
-
LOAD DATA
-
-
IF DATA START OF OBJECT
-
CREATE NEW OBJECT
-
STORE DATA IN OBJECT
-
ELSE IF DATA PART OF CURRENT OBJECT
-
STORE DATA IN OBJECT
-
ELSE
-
DISCARD DATA // or do something else with it
-
ENDIF
-
UNTIL NO MORE DATA
-
-
CLOSE FILE
- OPEN FILE //eg. istream i("filename");
-
-
LOOP
-
LOAD DATA //eg. i>>keyword;
-
-
IF DATA START OF OBJECT
-
CREATE NEW OBJECT //eg.istream& iss = i;
-
STORE DATA IN OBJECT //eg. iss>>keyword;
-
ELSE IF DATA PART OF CURRENT OBJECT
-
STORE DATA IN OBJECT //eg iss>>keyword
-
ELSE
-
//eg. return i; //which will return original object of istream
-
DISCARD DATA // or do something else with it
-
ENDIF
-
UNTIL NO MORE DATA
-
-
CLOSE FILE
If this is what you are talking about then bad luck, it still doesnt work. And if you are thinking something else please use (eg.) to give example of each step.
thanks.
Can you guys help????
Thankx
Banfa 9,065
Recognized Expert Moderator Expert - OPEN FILE //eg. istream i("filename");
-
-
LOOP
-
LOAD DATA //eg. i>>keyword;
-
-
IF DATA START OF OBJECT
-
CREATE NEW OBJECT //eg.istream& iss = i;
-
STORE DATA IN OBJECT //eg. iss>>keyword;
-
ELSE IF DATA PART OF CURRENT OBJECT
-
STORE DATA IN OBJECT //eg iss>>keyword
-
ELSE
-
//eg. return i; //which will return original object of istream
-
DISCARD DATA // or do something else with it
-
ENDIF
-
UNTIL NO MORE DATA
-
-
CLOSE FILE
If this is what you are talking about then bad luck, it still doesnt work.
No that is quite what I was thinking of, you have too many istreams. Using the 2 classes
istream - the input stream
myClass - class to hold the data in the file (defined in your original code)
then this is more what I had in mind - OPEN FILE //eg. istream i("filename");
-
-
LOOP
-
LOAD DATA FROM FILE //eg. i>>keyword;
-
-
IF DATA START OF OBJECT
-
CREATE NEW OBJECT OF myClass //eg. myObj = new myClass
-
STORE DATA IN OBJECT OF myClass //eg. keyword >> myObj
-
ELSE IF DATA PART OF CURRENT OBJECT
-
STORE DATA IN OBJECT OF myClass //eg keyword >> myObj
-
ELSE
-
DISCARD DATA // or do something else with it forget data in keyword
-
ENDIF
-
UNTIL NO MORE DATA
-
-
CLOSE FILE //eg. close i
-
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: Kamil |
last post by:
i dont know what i'm doing wrong i'm trying to get all the fields from a
specific row by user name i'm using php and i got the connection string down
and i made a query like this:
$query = mysql_query("SELECT * FROM <DBname> WHERE name = $_POST");
the variable $_POST was passed to the php code from a previous html
form i get the error:
Unknown column '<username here>' in 'where clause'
|
by: the_proud_family |
last post by:
HELP ME PLEASE!! my email is the_proud_family@yahoo.com
I can't get the ball to go up right side and then I need it to turn
around and keep turning until velocity=0 I have been at it for the
past 2 weeks now i give up and call for help. Please if anyone can
gide me through i will be so grateful!! I have pasted my code below
|
by: Peter |
last post by:
My problem is the last bit of coding below, the like statement does not
work. what I have is a product options field and in it is stored characters
i.e. "avcy" etc what the query does is that if I type in any single
character all the products that match the criteria should be displayed. So
if I typed in an "a" I should get 3 returns (see below) Fields could look
like this:
product.options:
bhw
djhsa
|
by: SK |
last post by:
Hi
I am trying to teach myself how to program in C. I am a physician
hoping to be able to help restructure my office. Anyhow, I amhoping
that the porblem I am having is simple to those much more experienced
in programming. I am trying to use the concept of arrays to calculate
the hours of my backoffice staff, however I am getting a ridiculous
amount of error lines. If any one has time to help me that would be
great. I am using the...
|
by: Daniel |
last post by:
I'm new to .Net and all of its abilities so I hope this makes sense.
Basically I'm confused on when is the appropriate time to use web forms
controls vs. regular HTML.
For example in ASP (non-.Net) if I wanted to fill a list it may look
something like this:
-------START CODE
<%
| |
by: Joner |
last post by:
Hello,
I'm having trouble with a little programme of mine where I connect to
an access database. It seems to connect fine, and disconnect fine, but
then after it won't reconnect, I get the error "operation is not
allowed when object is open" so I take out the line of code:
BookDetails.Connection1.Open
and it comes up with the error "operation is not allowed when object
|
by: deppy_3 |
last post by:
Hi.I am started learning Programm language C before some time.I am
trying to make a programm about a very simple "sell shop".This programm
hasn't got any compile problem but when i run it i face some other
ploblems which i can not correct.I would appreciated if someone take a
look at my programm so as to help me.I tried many times to find out
where my mistakes are but i didn't manage something.
I have made some comments due to the programm...
|
by: Rex |
last post by:
Hi All - I have a question that I think MIGHT be of interest to a
number of us developers. I am somewhat new to VIsual Studio 2005 but
not new to VB. I am looking for ideas about quick and efficient
navigating within Visual Studio 2005. Let's say your project (or
solution) has dozens of forms and hundreds or even thousands of
routines.
Two Questions:
1) BUILT-IN to Visual Studio 2005. What ideas do you have to quickly
|
by: HelpME |
last post by:
I wrote a program in Vb.Net that was running fine. However I am
unable to install it on a couple of machines. When i run it I get a
windows error message that says
My Project.exe has encountered a problem and needs to close. We are
sorry for the inconvience.
If you were in the middle of something, the information you were
working on might be lost
|
by: souporpower |
last post by:
Hello All
I am trying to activate a link using Jquery. Here is my code;
<html>
<head>
<script type="text/javascript" src="../../resources/js/
jquery-1.2.6.js"</script>
<script language="javascript" type="text/javascript">
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
| |
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
| |
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |