It contains a program that is given as command parameter the name of a text file, say, temp.dat. It will read each line of temp.dat and print out in correspondence the number of characters and words on that line. At the end it will print out the number of lines that were read.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 | /******************************************************* * MYCPLUS Sample Code - https://www.mycplus.com * * * * This code is made available as a service to our * * visitors and is provided strictly for the * * purpose of illustration. * * * * Please direct all inquiries to saqib at mycplus.com * *******************************************************/ #include <stdio.h> int main (int argc, char *argv[]){ FILE *fp; int nchars, nwords, nlines; int lastnblank; /* 0 iff the last character was a space */ char c; if(argc!=2){ printf("Usage: %s filename\n", argv[0]); exit(0); } if((fp=fopen(argv[1],"r"))==NULL){ perror("fopen"); exit(0); } nchars=nwords=nlines=lastnblank=0; while((c=getc(fp))!=EOF){ nchars++; if (c=='\n'){ if (lastnblank) nwords++; printf("words=%d, characters=%d\n", nwords, nchars); nchars=nwords=lastnblank=0; nlines++; }else{ if (((c==' ')||(c=='\t'))&(lastnblank)) nwords++; lastnblank=((c!=' ')&&(c!='\t')); } } printf("lines=%d\n", nlines); fclose(fp); } |