0

I want to count the numbers of words per line in a file using this function, @params line_number is the number of lines that the files contains.

When I try to parse this file :

world hello
hello world
hello hello

world salut

I get these results for the vector

vector[0] = 2
vector[1] = 2
vector[2] = 2
vector[3] = 1
vector[4] = 2

What is wrong with my code ? How can I stop getting 1 instead of 0 where there are no words on a line ?

int * get_vtor(FILE *file, int line_number)
{ 
  int *vtor = malloc(line_number*sizeof(int)), max, j, k, u;
  char a_file_line[TAILLE_MAX_LINE]; // TAILLE_MAX_LINE is #define earlier to 100
  j = k = u = max = 0;
  rewind(file);

   while((fgets(a_file_line, TAILLE_MAX_LINE ,file)) != NULL)
   { 
    char * current = strtok(a_file_line," "); 
    while(current != NULL)
    {
    u++;
    current = strtok(NULL, " ");
    }

    *(vtor+j) = u;
    u = 0; j++;
    memset(a_file_line, 0 , sizeof(a_file_line));
   }  

   return vtor;
}
maximegir
  • 63
  • 7

4 Answers4

1

Are you sure the line is really empty and doesn't have a newline character in it? When I tested strtok locally, it never returns an empty string. fgets doesn't strip the trailing newline from the string, though, so your code counts it as "one word."

You can fix this by changing your deliminter string in the strtok call to " \n" instead of " ".

Minor style note: I don't know who teaches this syntax, but it's unnecessarily hard to read:

*(vtor+j) = u;

You can write that more clearly as:

vtor[j] = u;
Joe Z
  • 17,413
  • 3
  • 28
  • 39
0

First guess, because you're reading standard input basically every line is ended with 'new line' character more detail solutions can be found here: Reading c file line by line using fgetc()

Community
  • 1
  • 1
Jeff
  • 11
  • 1
0

because you are using 'strtok', it always returns the whole string if there is no token in the first hit. so for the empty line you get '\n'.

just filter it and you will get the correct result.

while(current != NULL && *current != '\n')
WilliamX
  • 66
  • 3
0

Remove the newline after fgets:

while((fgets(a_file_line, 100 ,file)) != NULL)
   {   
    a_file_line[strlen(a_file_line) - 1] = 0;

It counts newlines as words because when you split a non-empty string, it will always have one token even if there is no delimiter.

perreal
  • 94,503
  • 21
  • 155
  • 181