StudyLover
  • Home
  • Study Zone
  • Profiles
  • Typing Tutor
  • Contact us
  • Sign in
StudyLover Program #40 (U4): File Copy & Text Statistics
Download
  1. C Programming
  2. C Programming Practice
Program #39 (U4): struct Student {roll, name, marks} with Pass/Fail
C Programming Practice

Task: Open a source and destination text file; copy character by character. While copying, count lines, words, and characters and print the summary.

 
#include <stdio.h>

#include <ctype.h>

 
int main(void){

    char src[256], dst[256];

    printf("Source file: "); scanf("%255s", src);

    printf("Destination file: "); scanf("%255s", dst);

 
    FILE *in = fopen(src, "r");

    if(!in){ printf("Cannot open source.\n"); return 0; }

    FILE *out = fopen(dst, "w");

    if(!out){ printf("Cannot open destination.\n"); fclose(in); return 0; }

 
    int ch, chars=0, words=0, lines=0, in_word=0;

    while((ch=fgetc(in))!=EOF){

        fputc(ch, out);

        ++chars;

        if(ch=='\n') ++lines;

        if(isalnum(ch)){ if(!in_word){ in_word=1; ++words; } }

        else{ in_word=0; }

    }

 
    fclose(in); fclose(out);

    printf("Copied. Lines=%d Words=%d Characters=%d\n", lines, words, chars);

    return 0;

}

 

End of document. 

Program #39 (U4): struct Student {roll, name, marks} with Pass/Fail
Our Products & Services
  • Home
Connect with us
  • Contact us
  • +91 82955 87844
  • Rk6yadav@gmail.com

StudyLover - About us

The Best knowledge for Best people.

Copyright © StudyLover
Powered by Odoo - Create a free website