Help me to find out what is wrong with this code

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • vermarajeev
    New Member
    • Aug 2006
    • 180

    Help me to find out what is wrong with this code

    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.

    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;
    }
    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
    Attached Files
  • vermarajeev
    New Member
    • Aug 2006
    • 180

    #2
    Hey sorry, there is some syntax error in that program.

    Code:
    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(line) );
    				continue;
    				}
    There should be "sizeof(line) instead of sizeof()", Please correct that and run the program.

    Comment

    • vermarajeev
      New Member
      • Aug 2006
      • 180

      #3
      Hi guys,
      Aah! I think I have confused you guys. Or is that the code seems confusing.

      Never mind. Let me clear. This is a simple program to read a text file. Firstly in main I call a method called "loadFile" which takes a stream as an argument.

      Code:
      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;
      }
      Then in method loadFile( t ), I just start reading the file till end. During this process, I check whether if I have read any "CONNECTION TABLE" which contains only digits in the file and if yes, then I create an object of myClass and allocate space for that, then I use overloaded operator ">>" defined in myClass to read that table(containin g digits). This operation is performed by the following code.

      Code:
      bool loadFile( std::istream &i )
      {
          if(i)
          {
            char line[256];
            int  p;
            char cp;		
            bool finished = false;	
            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(line) );
                  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;					
                  }
                 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;
      }
      After this because of operator overloading "<<", this part of code is called

      Code:
      std::istream& operator>>(std::istream& i, myClass& ct)
      {
          return ct.extractor( i );
      }
      Finally in ct.extractor(i) , Firstly I'm reading all the connection table(all digits),
      shown in this part of code defined in extractor(i)
      Code:
      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;
          }
      }
      Then, I check for various conditions, If I find the particular keyword I process that otherwise I put the keyword back to stream and return that stream. As in this part of code

      Code:
      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;
      }
      Now the problem is since the file is too large, the program reads the contents of the file(eugene.ct. syn) but not completely. Finally the program throws an exception. I'm using Visual C++ 2003 to run this program. My target is to read complete file without any runtime error.

      Please help me. I'm seeking your help at the earliest.

      Thanks,
      Rajeev

      Comment

      • Ashaman0
        New Member
        • Aug 2006
        • 7

        #4
        Nothing obviously wrong has popped out at me yet, ill try downloading your attachments to look at your code a little closer.

        What is the exception you are getting thou? And have you tried using a debuging utility to try and find out where the exception is being thrown and to see exactly what your code is actually doing?

        Comment

        • Ashaman0
          New Member
          • Aug 2006
          • 7

          #5
          hmm. I just compiled and ran your code on linux. Seems to work fine, no exceptions, and i think it ran all the way through.


          heres the end of the output. Is that right?
          Code:
          LIST OF EQUIVALENT ATOMS
          TOP[11] IS 11
          TOP[12] IS 11
          THERE IS ONE CHIRAL ATOM IN THE MOLECULE
          complexity = 9
          sumofcomplexity = 19
          
          END OF DUMP OF m[2].
          }
          CT OF REACTANT
          THIS IS THE CONNECTION TABLE OF m[3].
          THE PRODUCT IS 2
          THE COREACTANT IS 0
          firstreactant 4
          PRODUCTATOMS 2 1 3 4 6 7 5 8 9 10 11 12
          FUNGP 4: 132 7: 115
          
          REACTANTS WERE GENERATED 8293 TIMES
          5793 DUPLICATE MOLECULES WERE ELIMINATED.
          21 ROUTES WERE PROPOSED.
          THE PROGRAM HAS NOW FINISHED THE PROBLEM.
          
          Fri Sep 26 15:45:30 EDT 2003
          File loaded completely
          The only problem i had, was you were missing a variable on line 160.
          it was
          i.getline( line, sizeof( ) );
          needed to be
          i.getline( line, sizeof(line) );
          that should have not compiled thou, not thrown an exception. Maybe a typo?

          Comment

          • vermarajeev
            New Member
            • Aug 2006
            • 180

            #6
            Hi,
            Thanx for your reply

            That is what is more interesting about this code. Currently I'm using Microsoft Visual Studio .NET 2003 to run this program. When I run the same program on linux or Visual C++ Express Edition, it works fine.

            Yeah, there was a syntax error which I have already mentioned in my previous post.
            Try running the code on Microsoft Visual Studio .NET 2003 you will get an exception as "Run Time exception".

            Can you suggest me why is this unusual scenario occuring?????

            Thankx

            Comment

            • Banfa
              Recognized Expert Expert
              • Feb 2006
              • 9067

              #7
              Microsoft aren't any good at writing software :p

              Try running lint/splint over your code. If any bits are particularly complex (multiple levels of pointers to structures to pointers etc) then try re-writing them using additional local variables to simplify the code.

              Use the symbolic debugger to locate the exact error and line it is occuring on. It may give you a clue.

              Comment

              • vermarajeev
                New Member
                • Aug 2006
                • 180

                #8
                Hi Banfa,
                I was desperately waiting for your reply. Thanks for replying my post.

                Could you please tell me what is lint/splint???, I have absolutely no idea about that.
                Please explain me how I can correct that error.

                Thanks

                Comment

                • Banfa
                  Recognized Expert Expert
                  • Feb 2006
                  • 9067

                  #9
                  lint/splint/pclint are all versions (splint being free) of a utility that you can run on C/C++ code. This untility checks the code and reports Errors and Warnings much as a compiler would, however the errors and warnings it produces are normally much more detailed than most compilers and also contain information about errors that could occur at run time.

                  It is a useful to to ensure the rigerousness and robustness of your code.

                  If your code is working with 1 version of VC++ and not another version then one of the following errors has occured

                  1. You have used an extension that is only available in one of the versions (unlikely since it also works in Linux).

                  2. You have constructued the code in a way that causes a compiler bug to become apparent in the non-working version.

                  3. You have an error in the code that is not effecting the versions the code works on but is effecting the version that the code does not work on.

                  (sp)lint may help locate the problem if it is in category 3.

                  Using the symbolic debugger may help identify the location of the troublesome code, then visual examination may help fix the problem if it is in category 3 or if it is in category 2 then it may highlight a partiularly complex code syntax that could be simplified.

                  Good Luck, this is often a really hard type of bug to fix :-)

                  Comment

                  • vermarajeev
                    New Member
                    • Aug 2006
                    • 180

                    #10
                    Hi,
                    Glad to receive your reply :)

                    I just debuged the program and got to know that the stream which I'm using in my code, fails. As you can see the contents of the file "eugene", there are several strings like "Chiral", "available" , "CONNECTION TABLE" etc. As seen here for example

                    Code:
                    THIS IS STEP NUMBER 1
                    CONNECTION TABLE
                    35  0  0    6  0  0  0  0  0            0            0  0  1
                    17  0  0    6  0  0  0  0  0            0            0  0  1
                    8   4  0    5  5  0  0  0  0            0            0  0  1
                    8   0  1    5  0  0  0  0  0            0            0  0  0
                    6   4  0    3  3  4  6  0  0        1.037       0.6625  0  1
                    6   0  1    1  2  5  0  0  0        1.381         0.85  1  1
                    CHIRAL 6 C
                    available Name:		BROMOCHLOROACETIC ACID
                    available Supplier:		ALDRICH
                    available Price:		61.95USD/5g 97%
                    
                    CONNECTION TABLE
                    8   0  0    2  3  0  0  0  0            0            0  0  1
                    7   0  1    1  4  0  0  0  0            0            0  0  1
                    6   0  3    1  0  0  0  0  0            0            0  0  1
                    6   0  3    2  0  0  0  0  0            0            0  0  1
                    available Name:		N,O-DIMETHYLHYDROXYLAMINE HYDROCHLORIDE
                    available Supplier:		ALDRICH
                    available Price:		173.70USD/100g 98%
                    Now suppose I'm at below shown position in the file
                    Code:
                    CHIRAL 6 C
                    available Name:		BROMOCHLOROACETIC ACID
                    available Supplier:		ALDRICH
                    available Price:		61.95USD/5g 97%
                    So when I'm at this part of the program(as shown below)

                    Code:
                    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;
                    }
                    Here in the code above the origKeyword is "CHIRAL" which I convert to lowercase and check the various conditions which satisfies this condition
                    Code:
                    else if( !strncmp( keyword, "chiral", 6 ) )
                       {
                          i.getline(line, sizeof(line));
                       }
                    Then the next three line after "CHIRAL" in the file is "available" which too satisfies this part of code
                    Code:
                    else if( !strncmp( keyword, "available", 9 ) )
                       {
                          i.getline(line, sizeof(line));
                       }
                    Finally when I read the next string which is "CONNECTION TABLE", else part in my code is called which is
                    Code:
                     else
                       {
                          int len = strlen( origKeyword );
                          for(char c = origKeyword[len-1]; len>0; --len)
                          {
                             c = origKeyword[len - 1];				
                             i.putback(c);
                          }
                            finished = true;
                       }
                    here my len is 10, so char c = 'N' , hence 'N' is pushed back to stream. The stream now contains N ...(some data in the buffer).
                    Next c = 'O' is pushed stream contains ON...(some data in the buffer).
                    Next c = 'I' is pushed stream contains ION...(some data in the buffer).
                    Next c = 'T' is pushed stream contains TION...(some data in the buffer).
                    Next c = 'C' is pushed stream contains CTION...(some data in the buffer).
                    Next c = 'E' is pushed stream contains ECTION...(some data in the buffer).
                    Next c = 'N' is pushed stream contains NECTION...(some data in the buffer).

                    Now peroblem starts here,
                    When the next c = 'N' is pushed into the stream, stream still contains "NECTION...(som e data in the buffer)" instread of "NNECTION...(so me data in the buffer)". From here onwards if whatever is pushed onto the stream "i", It shows me "NECTION...(som e data in the buffer)". But the original sequence which I want is
                    Code:
                    "NNECTION...(some data in the buffer)"
                    "ONNECTION...(some data in the buffer)"
                    "CONNECTION...(some data in the buffer)"
                    Because the stream is not able to push back the character after N, it fails and throws exception.
                    Now, what can I do to solve this problem????. I just tried using
                    Code:
                    i.seekg(ios::cur, -len)
                    but this too fails, as the stream fails to return the correct position.
                    Please help me.

                    Comment

                    • MrJay
                      New Member
                      • Aug 2006
                      • 6

                      #11
                      I'm still waiting for any suggestions.

                      Thanks

                      Comment

                      • Banfa
                        Recognized Expert Expert
                        • Feb 2006
                        • 9067

                        #12
                        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.

                        Comment

                        • MrJay
                          New Member
                          • Aug 2006
                          • 6

                          #13
                          Please elaborate, I'm unable to get what you meant.

                          Comment

                          • vermarajeev
                            New Member
                            • Aug 2006
                            • 180

                            #14
                            Hi,
                            Please help me to solve that problem.

                            I'm in a big trouble.

                            Comment

                            • Banfa
                              Recognized Expert Expert
                              • Feb 2006
                              • 9067

                              #15
                              Sorry this must have slipped through the net.

                              What I mean is at the moment you code effective trys this

                              Code:
                              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

                              Code:
                              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


                              Code:
                              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.

                              Comment

                              Working...