Advertisement
Guest User

es4A.c

a guest
Apr 25th, 2019
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.15 KB | None | 0 0
  1. #include "es4A.h"
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. int wc(const char *filename, int *chars, int *words, int *lines){
  6.  
  7.   FILE* fp;
  8.   char* line_buf = NULL;
  9.   size_t line_buf_size = 0;
  10.   int lineCount = 0;
  11.   int charCount = 0;
  12.   int wordsCount = 0;
  13.   ssize_t line_size;
  14.   int i;
  15.   int lineCharCount;
  16.   int previousIsChar;
  17.  
  18.   printf("ciao\n");
  19.  
  20.   fp = fopen(filename, "r");
  21.   printf("ho aperto il file %s\n", filename);
  22.  
  23.   if(fp){
  24.  
  25.     line_size = getline(&line_buf, &line_buf_size, fp);
  26.  
  27.     while (line_size >= 0){
  28.       /*printf("linea %i: ci sono %zi caratteri\n", lineCount, line_size);*/
  29.  
  30.         lineCharCount = 0;
  31.         previousIsChar = 0;
  32.  
  33.         for(i = 0; i < line_size; i++){
  34.           printf("line_buf[%i]: %c\n", i, line_buf[i]);
  35.           lineCharCount += 1;
  36.           printf("ho appena aumentato il numero di caratteri in questa riga, ora ce ne sono: %i\n", lineCharCount);
  37.  
  38.           if (line_buf[i] == ' ' || line_buf[i] == '\n' || line_buf[i] == '\r' || line_buf[i] == EOF){
  39.             if (previousIsChar == 1){
  40.               wordsCount += 1;
  41.               printf("ho appena aumentato il numero di parole, ora sono %i\n", wordsCount);
  42.               previousIsChar = 0;
  43.             }
  44.           } else {
  45.             if(previousIsChar == 0 && i == line_size - 1){
  46.               wordsCount += 1;
  47.               printf("ho appena aumentato il numero di parole, ora sono %i\n", wordsCount);
  48.             }
  49.  
  50.             previousIsChar = 1;
  51.           }
  52.         }
  53.         if (lineCharCount > 0){
  54.           lineCount += 1;
  55.           printf("ho appena aumentato il numero di linee, ora sono %i\n", lineCount);
  56.           charCount +=lineCharCount;
  57.           printf("ho appena aumentato il numero di caratteri totali, ora sono %i\n", charCount);
  58.         }
  59.         line_size = getline(&line_buf, &line_buf_size, fp);
  60.     }
  61.  
  62.     printf("charCount = %i\n lineCount = %i\n wordsCount = %i\n", charCount, lineCount, wordsCount);
  63.  
  64.     fclose(fp);
  65.     *chars = charCount;
  66.     *lines = lineCount;
  67.     *words = wordsCount;
  68.     printf("sono arrivato alla fine\n");
  69.  
  70.     return 1;
  71.   } else {
  72.     return 0;
  73.   }
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement