English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

fopen() is used to write files in C

This is an example of printing the content of a file using the C language.

Assuming we have a file "new.txt" that contains the following content.

0, hell!o
1,hello!
2,gfdtrhtrhrt
3,demo

Now, let's look at an example.

Example

#include <stdio.h>
#include <conio.h>
void main() {
   FILE *f;
   char s;
   clrscr();
   f = fopen("new.txt", "r");
   while((s = fgetc(f)) != EOF) {
      printf("%c", s);
   }
   fclose(f);
   getch();
}

Output Result

0, hell!o
1,hello!
2,gfdtrhtrhrt
3,demo

In the above program, we have a text file "new.txt". The file pointer is used to open and read the file. It is displaying the content of the file.

FILE *f;
char s;
clrscr();
f = fopen("new.txt", "r");
while((s = fgetc(f)) != EOF) {
   printf("%c", s);
}