Hello!
I have a little problem with my code, I have been programming C for a couple of months now, and I need some help on this one I want to read a string from a file called Test.txt. Inside the file there is a string "Hello World!". And if that string is alredy there, the program should return 0 from the function read_file to read2, but it always returns 1, and in that way, it always writes "Hello World!" again to the file, which is not what I want..
#include <stdio.h>
FILE *file;
int read_file(char read [30]) {
file = fopen ("Test.txt", "rt");
fscanf(file, "%s" , read);
if (read == "Hello World!")
return 0;
else
return 1;
fclose(file);
}
int main (void) {
char read1 [30];
int read2;
read2=read_file(read1);
file = fopen ("Test.txt", "wt");
printf("%d \n", read2);
if (file == NULL)
printf("Can't open file. \n");
else if (read2 == 0)
printf ("That was alredy inside the file, closing...\n") && fclose(file);
else if (read2 == 1)
fprintf(file, "Hello World!") && printf ("Successfully written to the file. \n") && fclose(file);
getchar();
}