C Examples Read File
# C Example - Read a Line from a File
[ C Examples](#)
Read a line from a file.
Content of file tutorial.txt:
$ cat tutorial.txt google.com
## Example
#include#include // exit() function int main(){char c; FILE *fptr; if((fptr = fopen("tutorial.txt", "r")) == NULL){printf("Error! opening file"); // Exit if 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; }
Output:
Read content:
[ C Examples](#)
YouTip