Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use fopen to open file repeatedly in C

Tags:

c

fopen

I have a question about "fopen" function.

FILE *pFile1, *pFile2;
pFile1 = fopen(fileName,"rb+");
pFile2 = fopen(fileName,"rb+");

Can I say that pFile1==pFile2? Besides, can FILE type be used as a key of map?

Thanks!

like image 965
ZhiminXiang Avatar asked Jan 17 '26 18:01

ZhiminXiang


1 Answers

Can I say that pFile1 == pFile2?

No pFile1 and pFile2 are pointers to two distinct FILE structures, returned by the two different function calls.

Give it a try!!

To add further:

Note opening a file that is already open has implementation-defined behavior, according to the C Standard:

FIO31-C. Do not open a file that is already open

subclause 7.21.3, paragraph 8 [ISO/IEC 9899:2011]:

Functions that open additional (nontemporary) files require a file name, which is a string. The rules for composing valid file names are implementation-defined. Whether the same file can be simultaneously open multiple times is also implementation-defined.

Some platforms may forbid a file simultaneously being opened multiple times, but other platforms may allow it. Therefore, portable code cannot depend on what will happen if this rule is violated. Although this isn't a problem on POSIX compliant systems. Many applications open a file multiple times to read concurrently (of-course if you wants writing operation also then you may need concurrency control mechanism, but that's a different matter).

like image 84
Grijesh Chauhan Avatar answered Jan 19 '26 20:01

Grijesh Chauhan