C Program to print contents of file
fopen() is used to open and fclose() is used to close a file in C
#include <stdio.h> #include <stdlib.h> // For exit() int main() { FILE *fptr; char filename[100], c; printf("Enter the filename to open \n"); scanf("%s", filename); // Open file fptr = fopen(filename, "r"); if (fptr == NULL) { printf("Cannot open file \n"); exit(0); } // Read contents from file c = fgetc(fptr); while (c != EOF) { printf ("%c", c); c = fgetc(fptr); } fclose(fptr); return 0; } |
chevron_right
filter_none
Output:
Enter the filename to open a.txt /*Contents of a.txt*/
Recommended Posts:
- C program to copy contents of one file to another file
- C Program to merge contents of two files into a third file
- Write a C program that displays contents of a given file like 'more' utility in Linux
- C++ program to print unique words in a file
- Program to copy the contents of one array into another in the reverse order
- Print "Hello World" in C/C++ without using any header file
- C program to delete a file
- C++ program to create a file
- C Program to find size of a File
- LEX program to add line numbers to a given file
- C Program to count the Number of Characters in a File
- C Program to count number of lines in a file
- C++ program to append content of one text file to another
- C Program for Lower Case to Uppercase and vice-versa in a file
- Lex program to take input from file and remove multiple spaces, lines and tabs



