Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "es4A.h"
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- int wc(const char *filename, int *chars, int *words, int *lines){
- FILE* fp;
- char* line_buf = NULL;
- size_t line_buf_size = 0;
- int lineCount = 0;
- int charCount = 0;
- int wordsCount = 0;
- ssize_t line_size;
- int i;
- int lineCharCount;
- int previousIsChar;
- printf("ciao\n");
- fp = fopen(filename, "r");
- printf("ho aperto il file %s\n", filename);
- if(fp){
- line_size = getline(&line_buf, &line_buf_size, fp);
- while (line_size >= 0){
- /*printf("linea %i: ci sono %zi caratteri\n", lineCount, line_size);*/
- lineCharCount = 0;
- previousIsChar = 0;
- for(i = 0; i < line_size; i++){
- printf("line_buf[%i]: %c\n", i, line_buf[i]);
- lineCharCount += 1;
- printf("ho appena aumentato il numero di caratteri in questa riga, ora ce ne sono: %i\n", lineCharCount);
- if (line_buf[i] == ' ' || line_buf[i] == '\n' || line_buf[i] == '\r' || line_buf[i] == EOF){
- if (previousIsChar == 1){
- wordsCount += 1;
- printf("ho appena aumentato il numero di parole, ora sono %i\n", wordsCount);
- previousIsChar = 0;
- }
- } else {
- if(previousIsChar == 0 && i == line_size - 1){
- wordsCount += 1;
- printf("ho appena aumentato il numero di parole, ora sono %i\n", wordsCount);
- }
- previousIsChar = 1;
- }
- }
- if (lineCharCount > 0){
- lineCount += 1;
- printf("ho appena aumentato il numero di linee, ora sono %i\n", lineCount);
- charCount +=lineCharCount;
- printf("ho appena aumentato il numero di caratteri totali, ora sono %i\n", charCount);
- }
- line_size = getline(&line_buf, &line_buf_size, fp);
- }
- printf("charCount = %i\n lineCount = %i\n wordsCount = %i\n", charCount, lineCount, wordsCount);
- fclose(fp);
- *chars = charCount;
- *lines = lineCount;
- *words = wordsCount;
- printf("sono arrivato alla fine\n");
- return 1;
- } else {
- return 0;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement