Warning Nu esti autentificat. Te rog autentifica-te sau inregistreaza-te pentru a avea acces la toate facilitatile forumului.
SkullBox  
Noiembrie 23, 2008, 04:38:44 pm
Bine ai venit, Vizitator. Trebuie să te autentifici sau să îţi creezi un cont.
Ai pierdut sau nu ai primit emailul care conţine codul de activare al contului?

Autentifică-te cu numele de utilizator, parola şi precizează durata sesiunii.
Noutăţi: SmLex DeviantART
 
 SkullBoxDirector webTutoriale  Pagina principală   Ajutor Caută Autentificare Creează un cont  
Pagini: [1]
  Imprimă  
Subiect: Word, Line, Character counter  (Citit de 145 ori)
0Utilizatori şi 1 Vizitatori
redkar23
Global Moderator
Full Member
*****
Deconectat Deconectat

Mesaje: 151


WWW
Word, Line, Character counter, August 19, 2008, 11:43:54 am

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.

Cod:
#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
Memorat


What doesn't kill you, makes you stronger .       - Friedrich Nietzsche
O noua definitie a ironiei : life itself .                 - Redkar23
HostGator
Newbie
*
Mesaje: Multe

Reclamă cu aligatori
 

Ai auzit de aligatorul care ofera hosting?
 
   
Pagini: [1]
  Imprimă  
 
Schimbă forumul:  

Creat cu MySQL Creat cu PHP Ethical hacking and programming community Director web romanesc cu inscriere gratuita Validat cu XHTML 1.0! Validat cu CSS!
IPFind, FAQDB, LAMP.ro, Good Proxy, Aberez.EU, RoFreeSBIE, ShockingSoft.com, HostVision, Invatam.net, PC Troubleshooting, Curs valutar online
Powered by SMF 1.1.7 | SMF © 2006-2008, Simple Machines LLC
Traducerea în limba română © 2006-2007 www.smf.ro