I'm a frog dragon
I'm a frog dragon

Reputation: 8815

How to move file pointer to a particular location using fseek()?

My intention is to read every elements in the 2nd column into a buffer[] from the following .txt file:

9992891234 09.920 15.771 11.909
9992345971 07.892 12.234 09.234
9992348971 64.567 70.456 50.987
9992348231 89.234 85.890 58.982

I have know of a way to do it by using fscanf():

    for (int i=0;i<4;i++)
    {
    fscanf(pFile, "%lld", &junk);
    fscanf(pFile, "%f", &buffer[i]);
    fscanf(pFile, "%f", &junk);
    fscanf(pFile, "%f", &junk);
    }

However, since I'm doing parallel programing which requires me to use different Windows Threads to read different columns, so I'll need to read the elements in the 2nd column directly using fseek().

The question here is, what should I put in the 2nd argument in fseek() in the code below to move my file pointer to read the 2nd element of the 2nd line?

    fscanf(pFile, "%llf", &junk);//<------this is used to skip the 1st data

    for (int i=0;i<4;i++)
    {
            fscanf(pFile, "%f", &buffer[i]);
            fseek ( pFile , ??, SEEK_CUR );//<----how do I calculate this offset?
    }

Upvotes: 1

Views: 3799

Answers (2)

Paul R
Paul R

Reputation: 212949

You should do all the file reading in one thread and then if you really need to do "parallel programming" (homework assignment ?) you can then have separate threads accessing different parts of the data that you have read into memory via your file-reading thread.

Upvotes: 1

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 798536

Files only have a single read pointer. You're going to cause a ridiculous number of race conditions if you try to have multiple threads reading from the same file. Instead have a single thread be responsible for reading the file, parsing the line, and dispatching jobs.

Upvotes: 4

Related Questions