Skip to content

Instantly share code, notes, and snippets.

@schifano
Last active January 3, 2016 20:39
Show Gist options
  • Save schifano/8516219 to your computer and use it in GitHub Desktop.
Save schifano/8516219 to your computer and use it in GitHub Desktop.
C - reads and prints content from a file.
#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