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( "text.txt", "a+" )) == NULL )
printf("Can't open the file \"text.txt\"\n"
else
{
printf("Enter a string:\n"
gets( phrase );
rewind(fp);
if( Search( fp, phrase ) == false )
{
printf("I dont understand you.\n"
char* index = new char[10];
char* sNumber = new char[10];
sprintf( index,"%s ",ExtractIndex(fp));
printf("%s\n",index);
int k = 0;
for( int j = 1; index[j] != '\0'; j++ )
{
sNumber[k] = index[j];
k++;
}
index[2] = '\0';
printf("%s\n",index);
printf("sNumber = %s\n",sNumber);
int Number = atol( sNumber );
Number++;
printf("Number = %d",Number);
_itoa( Number, sNumber, 10 );
printf("\n sNumber = %s",sNumber);
sprintf( index, "%s",sNumber );
rewind(fp);
fputs( index, fp );
fputs( " ", fp );
for( int i = 0; phrase != '\0'; i++ )
fputc( phrase, fp );
fputc( '\n', fp );
delete index;
delete sNumber;
}
else
{
printf("I understand you !\n"
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("the initial position of the pointer is: %d\n",ftell(h));
printf("the size of the string is: %d\n",size);
fseek( h, -( size + 2 ), 1 );
printf("the position of the pointer now is: %d\n",ftell(h));
while(( c = getc(h)) != ' ' )
{
index = c; // this is the part that is causing the bug
i++;
}
index = '\0';
printf("the first word of line is \"%s\"\n",index);
return index;
delete index;
}
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( "text.txt", "a+" )) == NULL )
printf("Can't open the file \"text.txt\"\n"
else
{
printf("Enter a string:\n"
gets( phrase );
rewind(fp);
if( Search( fp, phrase ) == false )
{
printf("I dont understand you.\n"
char* index = new char[10];
char* sNumber = new char[10];
sprintf( index,"%s ",ExtractIndex(fp));
printf("%s\n",index);
int k = 0;
for( int j = 1; index[j] != '\0'; j++ )
{
sNumber[k] = index[j];
k++;
}
index[2] = '\0';
printf("%s\n",index);
printf("sNumber = %s\n",sNumber);
int Number = atol( sNumber );
Number++;
printf("Number = %d",Number);
_itoa( Number, sNumber, 10 );
printf("\n sNumber = %s",sNumber);
sprintf( index, "%s",sNumber );
rewind(fp);
fputs( index, fp );
fputs( " ", fp );
for( int i = 0; phrase != '\0'; i++ )
fputc( phrase, fp );
fputc( '\n', fp );
delete index;
delete sNumber;
}
else
{
printf("I understand you !\n"
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("the initial position of the pointer is: %d\n",ftell(h));
printf("the size of the string is: %d\n",size);
fseek( h, -( size + 2 ), 1 );
printf("the position of the pointer now is: %d\n",ftell(h));
while(( c = getc(h)) != ' ' )
{
index = c; // this is the part that is causing the bug
i++;
}
index = '\0';
printf("the first word of line is \"%s\"\n",index);
return index;
delete index;
}