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, x;
- int lineCharCount;
- int previousIsChar;
- printf("ciao\n");
- fp = fopen(filename, "r");
- x = 0;
- printf("ho aperto il file %s\n", filename);
- if(fp){
- line_size = getline(&line_buf, &line_buf_size, fp);
- while (line_size >= 0){
- lineCharCount = 0;
- previousIsChar = 0;
- /*printf("line_size: %zul\n", line_buf_size);*/
- for(i = 0; (size_t)i < line_buf_size; i++){
- lineCharCount += 1;
- if (line_buf[i] == ' ' || line_buf[i] == '\n' || line_buf[i] == '\r'){
- if (previousIsChar == 1){
- wordsCount += 1;
- previousIsChar = 0;
- }
- } else {
- if(previousIsChar == 0 && (size_t)i == line_buf_size - 1){
- wordsCount += 1;
- }
- previousIsChar = 1;
- }
- }
- if (lineCharCount > 0){
- lineCount += 1;
- charCount +=lineCharCount;
- }
- line_size = getline(&line_buf, &line_buf_size, fp);
- }
- 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