C Language Example - Read a Line from a File
Read a line from the file.
Content of the file example.txt:
$ cat example.txt example.com google.com
Example
#include <stdio.h>
#include <stdlib.h>// exit() function
int main()
{
char c[1000];
FILE *fptr;
if ((fptr = fopen("example.txt", "r")) == NULL)
{
printf("Error! opening file");
// Exit if the file pointer returns NULL
exit(1);
}
// Read text until a new line is encountered
fscanf(fptr,"%[^\n]", c);
printf("Read content:\n%s", c);
fclose(fptr);
return 0;
}
The output result is:
读取内容: example.comother extensions