Well im trying to find a string in a textfile but i have some problems :
When im trying to create a string called temp in the Compare function using the length of the string that was typed in to main to compiler gives me an error , and even if ill type in the code the size of temp the program will still not work . any1 have any ideas ?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <conio.h>
void Compare(FILE *filein,char *str,int length,char *source)
{
char temp[length];
fgets (temp,length,filein);
if (strcmp (temp,str) == 0)
{
printf ("The string : %s was found in the source file %s.\n",str,source);
printf ("Press any key to terminate the program...\n");
getch();
fclose(filein);
exit(0);
}
}
int main()
{
FILE *fin;
char str[40];
char source[40];
char ch;
int length;
printf ("Enter the string the you are looking for: ");
gets(str);
printf ("Enter the source file that will be searched: ");
gets(source);
fin = fopen(source,"rt");
length = strlen(str);
if (fin == NULL)
{
printf ("Failed to open source file..\n");
exit(1);
}
while (ch!=EOF)
{
ch = fgetc(fin);
if (ch == str[0])
Compare(fin,str,length,source);
}
printf ("The string %s was not found under the file %s\n",str,source);
printf ("Press any key to terminate the program...\n");
getch();
fclose(fin);
return 0;
}