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;
}