您可能想要这样的东西:
#include <stdio.h>
#include <stdlib.h>
int main()
{
FILE* fptr;
// Create the file and write to it
fptr = fopen("C:\\Users\\USER\\Downloads\\text.txt", "w");
fprintf(fptr, "Hello World\n");
fclose(fptr); // >>>>> don't forget to close the file <<<<<
// open the file we created before, and read one line from it
fptr = fopen("C:\\Users\\USER\\Downloads\\text.txt","r");
char input[100];
fgets(input, 100, fptr);
fclose(fptr);
printf("%s", input);
}
fopen
NULL
,这表明由于某种原因无法打开文件(最常见的原因之一是该文件不存在)。