Login to ask a question from Data Files

Write a C Program to count the Number of Characters in a File

#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; ....

Show More
Previous Question Next Question
🔗

Similar Questions

From Data Files · C programming

View All
WhatsApp