Am scris codul pentru un student, si ma gandeam ca poate ii va fi de folos cuiva. Programul numara numarul de cuvinte, linii si caractere din mai multe fisiere. Prin cuvinte intelegem orice are la extremitati spatiu. Este ceva de genul programului WC din unix, doar ca nu complet. In plus acest cod e limitat ( extensii, numar de argumente ) . Nu pot sa spun ca e bug-free fiindca nu mi-am batut capul prea tare, programul nefiind important.
#include
#include
#include // used just for the function isalpha() that checks if a character is a letter
int i,j; // just two iterator variables
long table[401][3]; // the table that will hold the information needed
// table[x][0] = number of lines
// table[x][1] = number of words
// table[x][2] = number of characters
// x = index of the arguments
FILE *file_handle; // the file handle through which the program accesses the content of a file
int file_index; // this is a counter to keep track of the processed files
char c;
long sum;
int process_file(){
int is_word=0; // if this variable is 1, then we currently read a word
fscanf(file_handle,"%c",&c); // reading char by char
while(!feof(file_handle)){ // until we find the end of the file
table[file_index][2]++; // count up character number
switch(c){
case '\n': //if the character is newline
if(is_word){ // if the previously was a word
table[file_index][1]++; // increment word count
}
is_word = 0; // the currently read was not a word
table[file_index][0]++;break; // increment line count
case ' ' : // if character is whitespace
if(is_word) { // if the previously was a word
table[file_index][1]++; // increment word count
}
is_word = 0; // the currently read was not a word
break;
default:is_word = 1; // anything else read is part of a word
}
fscanf(file_handle,"%c",&c); // read char
}
file_index++; // move to the next row in table
return 0;
}
int main(int argcount,char *argv[]){
int len;
argcount--; //This is the argument count, and I've substracted 1 because
//the first argument is the file's name ( we don't need that )
if(!argcount) { // if the argument count is 0 print error message
printf("Usage : ./wc ...\n");
return 0;
}
if(argcount > 400) { // if the argument count is over 400, print error message
printf("You can have maximum 400 file checked.\n");
return 0;
}
for(i=1;i<=argcount;i++){ //for every argument
len=strlen(argv[i]); // verify length of the argument
if( len < 3){ // must be at least 3 ,else print error message
printf("The minimum length for an argument is 3.\n");
return 0;
}
if ( tolower(argv[i][len-1]) != 'c' || argv[i][len-2] != '.') { // must have .c extension, else print error messsage
printf("Argument does not have extension .c\n");
return 0;
}
file_handle=fopen(argv[i],"r"); // open the current file for processing
if(!file_handle){ // if the file doesn't exist
printf("No such file.\n"); // print error message and exit
return 0;
}
process_file(); // processing function, see above
fclose(file_handle); // free the file_handle variable for further use
}
for(i=0;i for(j=0;j<3;j++) // print the line count, word count and character count
printf("%d ",table[i][j]);
printf("%s\n",argv[i+1]); // print the file name
}
if(argcount>1){ // if there are more arguments
for(j=0;j<3;j++){
for(i=0;i sum+=table[i][j]; // make the total
printf("%d ",sum); // and print
sum=0; // reset the sum variable
}
printf("total\n"); // print that total, from wc :D
}
return 0;
}Cheers, Redkar23