473,804 Members | 3,953 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Help me to find out what is wrong with this code

180 New Member
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.

Expand|Select|Wrap|Line Numbers
  1. #include<iostream>
  2. #include<fstream>
  3. using namespace std;
  4.  
  5. class myClass
  6. {
  7. public:
  8.     myClass(){}
  9.     friend std::istream& operator>>(std::istream& i, myClass& ct);
  10.     std::istream& extractor(std::istream& i);
  11. };
  12.  
  13. std::istream& myClass::extractor(std::istream& i)
  14. {
  15.     char line[256];
  16.     char keyword[64];
  17.     char origKeyword[64];
  18.     int  p;
  19.     char cp;
  20.     bool finished = false;
  21.     bool endct = false;    
  22.  
  23.     while( i && !finished )
  24.         {
  25.             if( (p = i.peek()) == ';' || p == '#' )
  26.             {
  27.             // read the comment line and discard it
  28.             i.getline( line, 255 );
  29.             continue;
  30.             }        
  31.             // check for eof or error
  32.             if( !i )
  33.                 continue;
  34.             while( i && isspace( p = i.peek() ) )
  35.             {
  36.             i.get(cp);
  37.             }
  38.             // check for error or eof
  39.             if( !i )
  40.                 continue;
  41.  
  42.             if( !endct )
  43.             {
  44.             // check for a digit, and if we don't get one it means we
  45.             // are done with tablecounting
  46.                 if( ( p = i.peek() ) != EOF && isdigit( p ) )
  47.                 {
  48.                     i.getline(line, sizeof(line));    
  49.                     continue;
  50.                 }
  51.                 else
  52.                 {
  53.                 // this is the end of the connection table proper
  54.                 endct = true;
  55.                 }
  56.             }
  57.  
  58.             if( ( p = i.peek() ) != EOF && isalpha( p ) )
  59.             {
  60.             // read the keyword, and deal with the input
  61.             i >> origKeyword;
  62.             // make a copy of the keyword, in case we need to put it back in the stream
  63.             strcpy( keyword, origKeyword );
  64.             // change to lower case
  65.             for( char *lp = keyword; *lp; ++lp )
  66.             {
  67.             *lp = tolower( *lp );
  68.             }
  69.             if( i )
  70.                 {
  71.                     if( !strncmp( keyword, "cis", 3 ) )
  72.                     {
  73.                         i.getline(line, sizeof(line));
  74.                     }
  75.                     else if( !strncmp( keyword, "trans", 5 ) )
  76.                     {
  77.                         i.getline(line, sizeof(line));
  78.                     }
  79.                     else if( !strncmp( keyword, "entgegen", 8 ) )
  80.                     {
  81.                         i.getline(line, sizeof(line));
  82.                     }
  83.                     else if( !strncmp( keyword, "zusammen", 8 ) )
  84.                     {
  85.                         i.getline(line, sizeof(line));
  86.                     }
  87.                     else if( !strncmp( keyword, "chiral", 6 ) )
  88.                     {
  89.                         i.getline(line, sizeof(line));
  90.                     }
  91.                     else if( !strncmp( keyword, "donttouch", 9 ) )
  92.                     {
  93.                         i.getline(line, sizeof(line));
  94.                     }
  95.                     else if( !strncmp( keyword, "startwith", 9 ) )
  96.                     {
  97.                         i.getline(line, sizeof(line));
  98.                     }
  99.                     else if( !strncmp( keyword, "keybond", 7 ) )
  100.                     {
  101.                         i.getline(line, sizeof(line));
  102.                     }
  103.                     else if( !strncmp( keyword, "planetrans", 10 ) )
  104.                     {
  105.                         i.getline(line, sizeof(line));
  106.                     }
  107.                     else if( !strncmp( keyword, "planecis", 8 ) )
  108.                     {
  109.                         i.getline(line, sizeof(line));
  110.                     }
  111.                     else if( !strncmp( keyword, "available", 9 ) )
  112.                     {
  113.                         i.getline(line, sizeof(line));
  114.                     }
  115.                     else
  116.                     {
  117.                         int len = strlen( origKeyword );
  118.                         for(char c = origKeyword[len-1]; len>0; --len)
  119.                         {
  120.                             c = origKeyword[len - 1];                            
  121.                             i.putback(c);
  122.                         }
  123.                         finished = true;
  124.                     }
  125.                 }//if i
  126.                 continue;
  127.             }
  128.  
  129.         if( EOF == i.peek() )
  130.             break;
  131.         }//while
  132.         return i;
  133. }
  134.  
  135. std::istream& operator>>(std::istream& i, myClass& ct)
  136. {
  137.     return ct.extractor( i );
  138. }
  139.  
  140. bool loadFile( std::istream &i )
  141. {
  142.     if(i)
  143.     {
  144.         int ii = 1;
  145.         char line[256];
  146.         int  p;
  147.         char cp;        
  148.         bool finished = false;
  149.         int  routesw = 2;        // routesw == 2 means the beginning of the file
  150.  
  151.         int  ctsw = 0;
  152.         int  infosw = 0;        
  153.         myClass* ct = NULL;
  154.  
  155.         while( i && !finished )
  156.             {
  157.                 if( (p = i.peek()) == ';' || p == '#' )
  158.                 {
  159.                 // read the comment line and discard it
  160.                 i.getline( line, sizeof() );
  161.                 continue;
  162.                 }
  163.  
  164.                 // check for eof or error
  165.                 if( !i )
  166.                     continue;
  167.  
  168.                 // If we haven't reached EOF, read the line, and parse.
  169.                 // Otherwise break the loop and finish.
  170.                 // The getline() function reads to the '\n' (or sizeof(line) - 1),
  171.                 // discards it, and appends a '\0' to the line.
  172.                 if( ( p = i.peek() ) != EOF )
  173.                 {
  174.                     i.getline( line, sizeof(line) );
  175.                     cout<<line<<endl;    
  176.                     if(ii == 4017)
  177.                     {
  178.                         cout<<ii<<endl;
  179.                     }
  180.                     ii++;
  181.                 }
  182.                 else
  183.                     break;
  184.                 if(i)
  185.                 {
  186.                     if( strstr(line,"CONNECTION TABLE") )
  187.                     {
  188.                         // The connection table starts on the next line,
  189.                         // and so we read it here. 
  190.                         // skip blank space at the beginning of the line
  191.                         while( i && isspace( p = i.peek() ) )
  192.                         {
  193.                             i.get(cp);
  194.                         }
  195.  
  196.                         // check for error or eof
  197.                         if( !i )
  198.                             continue;
  199.  
  200.                         // Check for a digit, meaning we are about to read a 
  201.                         // ConnectionTable.  Only allocate a new ct if we
  202.                         // are not at EOF and the next line begins with a digit.
  203.                         if( ( p = i.peek() ) != EOF && isdigit( p ) )
  204.                         {
  205.                             // Allocate a new connection table
  206.                             ct = new myClass;                                        
  207.                              i >> *ct;                                        
  208.                         }
  209.                         i.clear();
  210.                         // if we are at EOF then break
  211.                         if( EOF == i.peek() )
  212.                         {
  213.                             finished = true;
  214.                             break;
  215.                         }
  216.                     }                    
  217.                 } //if(i)
  218.                 // check whether we are at the end of the file
  219.             if( EOF == i.peek() )
  220.                 break;
  221.             }//while
  222.     return true;
  223.     }//if(i)
  224.     return false;
  225. }
  226.  
  227. int main(int argc, char* argv[])
  228. {
  229.     ifstream t("eugene.ct.syn", std::ios::in );
  230.     if(t)
  231.     {
  232.         t.seekg( 0, std::ios::beg );
  233.         if(loadFile( t ))
  234.         {
  235.             cout<<"File loaded completely"<<endl;
  236.         }
  237.         else
  238.             cout<<"File failed to load"<<endl;
  239.     }
  240.     t.close();
  241.  
  242.     return 0;
  243. }
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
MrJay
6 New Member
I'm still waiting for any suggestions.

Thanks
Sep 1 '06 #11
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.
Sep 1 '06 #12
MrJay
6 New Member
Please elaborate, I'm unable to get what you meant.
Sep 2 '06 #13
vermarajeev
180 New Member
Hi,
Please help me to solve that problem.

I'm in a big trouble.
Sep 4 '06 #14
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

Expand|Select|Wrap|Line Numbers
  1. OPEN FILE
  2.  
  3. LOOP
  4.   LOAD DATA
  5.   IF OBJECT FOUND CALL OBJECT LOAD DATA FUNCTION
  6. UNTIL NO MORE DATA
  7.  
  8. CLOSE FILE
  9.  
and OBJECT LOAD DATA FUNCTION does this

Expand|Select|Wrap|Line Numbers
  1. LOOP
  2.   LOAD DATA
  3.   IF PART OF THIS OBJECT
  4.     STORE DATA
  5.   ELSE
  6.     PUT DATA BACK   // This is not working
  7.   ENDIF
  8. UNTIL START OF NEXT OBJECT FOUND
  9.  
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


Expand|Select|Wrap|Line Numbers
  1. OPEN FILE
  2.  
  3. LOOP
  4.   LOAD DATA
  5.  
  6.   IF DATA START OF OBJECT
  7.     CREATE NEW OBJECT
  8.     STORE DATA IN OBJECT
  9.   ELSE IF DATA PART OF CURRENT OBJECT
  10.     STORE DATA IN OBJECT
  11.   ELSE
  12.     DISCARD DATA  // or do something else with it
  13.   ENDIF
  14. UNTIL NO MORE DATA
  15.  
  16. CLOSE FILE
  17.  
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.
Sep 7 '06 #15
vermarajeev
180 New Member
Thankx Banfa,
I'll start my implementation very soon. If any problem I'll let you know.

Rajeev
Sep 8 '06 #16
vermarajeev
180 New Member
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

Expand|Select|Wrap|Line Numbers
  1. OPEN FILE
  2.  
  3. LOOP
  4.   LOAD DATA
  5.  
  6.   IF DATA START OF OBJECT
  7.     CREATE NEW OBJECT
  8.     STORE DATA IN OBJECT
  9.   ELSE IF DATA PART OF CURRENT OBJECT
  10.     STORE DATA IN OBJECT
  11.   ELSE
  12.     DISCARD DATA  // or do something else with it
  13.   ENDIF
  14. UNTIL NO MORE DATA
  15.  
  16. CLOSE FILE
Expand|Select|Wrap|Line Numbers
  1. OPEN FILE //eg. istream i("filename");
  2.  
  3. LOOP
  4.   LOAD DATA    //eg. i>>keyword;
  5.  
  6.   IF DATA START OF OBJECT 
  7.     CREATE NEW OBJECT    //eg.istream& iss = i;
  8.     STORE DATA IN OBJECT //eg. iss>>keyword;
  9.   ELSE IF DATA PART OF CURRENT OBJECT
  10.     STORE DATA IN OBJECT  //eg iss>>keyword
  11.   ELSE
  12.                    //eg. return i; //which will return original object of istream
  13.     DISCARD DATA  // or do something else with it
  14.   ENDIF
  15. UNTIL NO MORE DATA
  16.  
  17. 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.
Sep 8 '06 #17
vermarajeev
180 New Member
Can you guys help????

Thankx
Oct 19 '06 #18
Banfa
9,065 Recognized Expert Moderator Expert
Expand|Select|Wrap|Line Numbers
  1. OPEN FILE //eg. istream i("filename");
  2.  
  3. LOOP
  4.   LOAD DATA    //eg. i>>keyword;
  5.  
  6.   IF DATA START OF OBJECT 
  7.     CREATE NEW OBJECT    //eg.istream& iss = i;
  8.     STORE DATA IN OBJECT //eg. iss>>keyword;
  9.   ELSE IF DATA PART OF CURRENT OBJECT
  10.     STORE DATA IN OBJECT  //eg iss>>keyword
  11.   ELSE
  12.                    //eg. return i; //which will return original object of istream
  13.     DISCARD DATA  // or do something else with it
  14.   ENDIF
  15. UNTIL NO MORE DATA
  16.  
  17. 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

Expand|Select|Wrap|Line Numbers
  1. OPEN FILE //eg. istream i("filename");
  2.  
  3. LOOP
  4.   LOAD DATA FROM FILE   //eg. i>>keyword;
  5.  
  6.   IF DATA START OF OBJECT 
  7.     CREATE NEW OBJECT OF myClass   //eg. myObj = new myClass
  8.     STORE DATA IN OBJECT OF myClass //eg. keyword >> myObj
  9.   ELSE IF DATA PART OF CURRENT OBJECT
  10.     STORE DATA IN OBJECT OF myClass //eg keyword >> myObj
  11.   ELSE
  12.     DISCARD DATA  // or do something else with it forget data in keyword
  13.   ENDIF
  14. UNTIL NO MORE DATA
  15.  
  16. CLOSE FILE  //eg. close i
  17.  
Oct 19 '06 #19

Sign in to post your reply or Sign up for a free account.

Similar topics

8
2338
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'
1
1957
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
9
4636
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
27
2125
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...
2
2065
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 <%
13
4341
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
83
7882
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...
16
2824
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
6
1648
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
53
8425
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">
0
9706
marktang
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...
0
10580
Oralloy
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...
0
10335
jinu1996
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...
1
10323
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,...
0
10082
tracyyun
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...
0
9157
agi2029
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...
0
6854
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();...
0
5652
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4301
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

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.