Last active
January 3, 2016 20:39
-
-
Save schifano/8516219 to your computer and use it in GitHub Desktop.
C - reads and prints content from a file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
int main(void) | |
{ | |
FILE *fp; /* FILE pointer to keep track of file being accessed */ | |
int c; | |
/* If file is empty, print error message */ | |
if (fp == NULL) { | |
printf("File not found."); | |
return 1; | |
} | |
/* PRINT FILE */ | |
fp = fopen("test.txt","r"); /* reads content of file */ | |
while(1) | |
{ | |
c = fgetc(fp); | |
if(feof(fp)) /* end of file */ | |
{ | |
break ; | |
} | |
printf("%c", c); | |
} | |
printf("\n"); /* Add linebreak to separate text from command line prompt */ | |
fclose(fp); /* CLOSE file */ | |
return(0); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment