Write a C Program to print contents of file.
Write a C Program to count the Number of Characters in a File
Show More#include
#include // For exit()
#define MAX_FILE_NAME 100
void main()
{
FILE* fp;
int count = 0;
char filename[MAX_FILE_NAME];
printf("Enter file name: ");
scanf("%s", filename);
// Open the file
fp = fopen(filename, "r");
// Check if file exists
if (fp == NULL) {
printf("Could not open file %s",
filename);
exit(0);
}
// Extract characters from file
// and store in character c
for (c = getc(fp); c != EOF; c = getc(fp))
{
// Increment count for this character
count = count + 1; ....
All Chapters — C programming
Similar Questions
From Data Files · C programming