Let's assume I've got a file
a
ab
abc
abcd
abcde
abcdef
abcdefg
abcdefgh
abcdefgh
abcdefg
abcdef
abcde
abcd
abc
ab
a
I need to find lines with length >= 5
.
Here is the code
int cOriginal(char *fileName)
{
FILE *file = fopen(fileName, "r");
char line[256];
int numberOfStrings = 0;
while(fgets(line, sizeof(line), file))
{
if (strlen(line) >= sizeCap)
{
numberOfStrings++;
}
}
fclose(file);
return numberOfStrings;
}
where sizeCap
is a constant, defined as
const int sizeCap = 5;
My main()
function
int main()
{
char filename[] = "file.txt";
int cnt3 = cOriginal(filename); // th
printf("Using function3: %d", cnt3);
return 0;
}
The problem is that it returns 10
instead of 8
. And I don't get the idea why.
I don't have an IDE beside me so I have to use g++ and command line to compile it so I cannot debug it (or at least I don't know how).
What is the problem?