Weird behavior of ftell and fseek reading a txt file

575 Views Asked by At

I'm trying to read a txt file containing a "picture" (a matrix of chars) and ftell and fseek seem to be working in a different way than what i was taught in class and on the web.

Here is the code:

fp = fopen(filename, "r");
checkFopen(fp);

findRowsAndCols(fp, &rows, &cols);

for (i = 0; i < cols; i++)
    {
        fseek(fp, i, SEEK_SET);
        for (j = 0; j < rows; j++)
        {
            ch = fgetc(fp);
            if (ch != ' ')
            {
                //doing something with ch..
            }

            test1 = fseek(fp, (cols-1), SEEK_CUR);
            test2 = ftell(fp);
        }
    }
//cols = 9 (int)

Instead of jumping 8 characters in the file the cursor is moved only by 3 test1 is always 0 while test2 is increased twice by 4 than by 5 and by 6..

This is a very weird behavior for a program in my opinion. What am I doing wrong?

Edit:

the text file:

12345678\n

9!@#____\n

________\n

________\n

________\n

I'm trying to read the file char by char going through each column from top to bottom.

The first char I get is '1' then '5' and then '\n'

0

There are 0 best solutions below