Hi,
I am using fgets in my program to count the number of lines in file. The number of columns in each line is entered by the user. But the number of lines are not known. After counting the number of lines in the file i am again using it to store each line of the file in a two dimensional array.
The problem is the number of lines in my file is 4. however the number of lines counted by fgets() is 8. Following is my code.
fscanf(inFile2, "%d", &numCols);
fscanf(inFile2, "%c", &nullchar);
int linenumber = 0;
char line[numCols];
while (fgets(line, (numCols+1), inFile2) != NULL)
{
fgets(line, (numCols), inFile2);
if(line[0] == 'E')
break;
linenumber++;
}
fclose(inFile2);
fprintf(stderr, "numCols = %d, Linenumber = %d", linenumber);
FILE *inFile3 = fopen(tName, "r");
for(int ll=0; ll<linenumber; ll++)
{
fgets(line, (numCols+1), inFile3);
for(l = 0; l < strlen(line); l++)
{
if(line[l] == '0')
{
test_vector[ll][l] = 0;
}
if(line[l] == '1')
{
test_vector[ll][l] = 1;
}
if(line[l] == 'X')
{
test_vector[ll][l] = 2;
}
}
}
fclose(inFile3);
-----------------------------------------------------------
My file looks something like this
5
10101
10100
00010
10101
END
The value of linenumber is not 4.
Thanks