C open file multiple times

polslinux picture polslinux · Mar 25, 2013 · Viewed 18.2k times · Source

I have a file opened with fopen. There is a way to reopen the same file (while it is opened) but have a different seek? (so i can use fread independently)

Answer

MOHAMED picture MOHAMED · Mar 25, 2013

there is no problem if you keep reading only.

Be careful if you write in the file especially if you have 2 threads that access with read/write to the file at the same time

If your code looks like that

FILE *fp1, *fp2;

fp1 = fopen("file", "r");
fp2 = fopen("file", "r");

then you have 2 seeks in the same file. and the position of seeks are independent. reading from fp1 does not have any impact in fp2