Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Bug problem 1

Status
Not open for further replies.

Leibnitz

Programmer
Apr 6, 2001
393
CA
I'am writing a little A.I application,every things works fine except that i'am geting a bug everytime i launch the program,i know where the bug is,but i dont know how to make a correction.I dont even know why it is causing a bug.

Here is the code:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

bool Search( FILE *f, char *string );
int FindPos( FILE *g, int currentpos );
char *ExtractIndex( FILE *h );

void main()
{
FILE *fp;
char *phrase = new char[100];
if((fp = fopen( &quot;text.txt&quot;, &quot;a+&quot; )) == NULL )
printf(&quot;Can't open the file \&quot;text.txt\&quot;\n&quot;);
else
{
printf(&quot;Enter a string:\n&quot;);
gets( phrase );
rewind(fp);
if( Search( fp, phrase ) == false )
{
printf(&quot;I dont understand you.\n&quot;);
char* index = new char[10];
char* sNumber = new char[10];
sprintf( index,&quot;%s &quot;,ExtractIndex(fp));
printf(&quot;%s\n&quot;,index);
int k = 0;
for( int j = 1; index[j] != '\0'; j++ )
{
sNumber[k] = index[j];
k++;
}
index[2] = '\0';
printf(&quot;%s\n&quot;,index);
printf(&quot;sNumber = %s\n&quot;,sNumber);
int Number = atol( sNumber );
Number++;
printf(&quot;Number = %d&quot;,Number);
_itoa( Number, sNumber, 10 );
printf(&quot;\n sNumber = %s&quot;,sNumber);
sprintf( index, &quot;%s&quot;,sNumber );
rewind(fp);
fputs( index, fp );
fputs( &quot; &quot;, fp );
for( int i = 0; phrase != '\0'; i++ )
fputc( phrase, fp );
fputc( '\n', fp );
delete index;
delete sNumber;
}
else
{
printf(&quot;I understand you !\n&quot;);
ExtractIndex(fp);
}
fclose(fp);
delete phrase;
}
}

bool Search( FILE *f, char *string )
{
char *sentence;
sentence = new char[100];
bool found = false;
while( fgets( sentence, 100, f ) != NULL )
{
if( strstr( sentence, string ) != NULL )
{
found = true;
break;
}
}
delete sentence;
return found;
}

int FindPos( FILE *g, int currentpos )
{
char c;
int pos = 0;
rewind(g);
while(( c = getc(g)) && ftell(g) != currentpos )
{
pos++;
if( c == '\n' )
pos = 0;
}
return pos;
}

char *ExtractIndex( FILE *h )
{
int size = FindPos( h, ftell(h));
char *index = new char[10];
char c;
int i = 0;
printf(&quot;the initial position of the pointer is: %d\n&quot;,ftell(h));
printf(&quot;the size of the string is: %d\n&quot;,size);
fseek( h, -( size + 2 ), 1 );
printf(&quot;the position of the pointer now is: %d\n&quot;,ftell(h));
while(( c = getc(h)) != ' ' )
{
index = c; // this is the part that is causing the bug
i++;
}
index = '\0';
printf(&quot;the first word of line is \&quot;%s\&quot;\n&quot;,index);
return index;
delete index;
}
 
You are reading past the end of your file or some other file error. Try adding this to your function, it will show you the error. Notice the value of &quot;i&quot; when you debug, it's huge.

while(( c = getc(h)) != ' ' )
{
if (feof(h))
{
printf(&quot;error reading file\n&quot;);
exit(0);
}

index = c;
i++;
}

THE SOLUTION: change your while statement to this:

while(( c = getc(h)) != EOF )

It will work great then.
 
Thanks for the sugestions.
if (feof(h))
{
printf(&quot;error reading file\n&quot;);
exit(0);
}
works fine with the rest of the code.
I'am no longer having a bug.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top