I have written this function which reads a file, compares a string to each line and if the line contains the string, it return it.
char* FileSearch2 (char* File_Name , char* String) {
FILE * T = fopen(File_Name, "rt");
char* Line = (char*) malloc (sizeof(char*) * 1024);
strcat(String,"\t");
if (T) {
while (fgets(Line,1024,T)) {
if (strstr(Line, String) != NULL) {
fclose(T);
return Line;
}
}
}
fclose(T);
return "0";
}
The problem is that, the second time I run this function it always returns "0".
For example
char* FirstRun = FileSearch2 ("File.txt", Value); // Assuming the value is "Hello", it returns the line
Now
char* SecondtRun = FileSearch2 ("File.txt", Value); // Assuming the value is "Hello", it returns "0"
I would like to know what exactly I am doing wrong.