Good evening,
I know there's similar questions, which I have been sifting through, but this particular issue seems to be unique.
I am attempting to figure out how to simply read a text file into a string, and then write the string to standard output. I tried this code, but nothing is happening in the console when I call puts(). The file.txt generates properly with "hello" written, but the last if statement doesn't seem to work for some reason since it does not reach my test condition. How can I make this functional?
This was the code given by many, many examples online, only slightly modified:
#include <stdio.h>
int main()
{
FILE *fp;
char str[60];
fp = fopen("file.txt","w+");
fprintf(fp,"%s","hello");
if(fp==NULL){
perror("Error opening file");
return(-1);
}
if (fgets (str, 60, fp)!=NULL)
puts(str);
printf("%s","test");
fclose(fp);
return 0;
}