file example in c

Example 1: c program to read and write to a file

/*Program to read from file using getc() function*/
#include <stdio.h>
int main() {
 FILE *fp;
 char ch;
 /*Open file in read mode*/
 fp= fopen ('example.txt', 'r');
 while( (ch = getc(fp)) != EOF) {
  /*getc() function reads a character and its value is stored in variable 'ch' until EOF is encountered*/
  printf('%ch', ch);
 }
  fclose(fp);
  return 0;
}

Example 2: c file

#include <stdio.h>
#include <stdlib.h>

int main() {
   	printf("Hello Word!");
    return 0;
}

Tags:

C Example