Checking if two FILE *s are associated with the same stream.

E

Edd

Hello,

I have an array of strings containing filenames. I must open each in turn
and parse the data within. However, if a filename appears multiple times in
the list it must still only be read and parsed once.

Initially, to solve this problem I thought strcmp'ing the filenames would
suffice to check for duplicates in the list. However, it may be the case
that "somefile.in" identifies the same file as "~/inputs/somefile.in",
despite the strings being different. Thus simply using strcmp is inadequate.

Assuming:

FILE *fptr1 = fopen("somefile.in", "rb");
FILE *fptr2 = fopen("~/inputs/somefile.in", "rb");

both return non-NULL, is there a portable way of checking that fptr1 and
fptr2 are associated with the same stream?
As a really nasty hack, I could check equality of the appropriate fields in
the FILEs. But that probably wouldn't port outside of a given implementation
of the standard library, let alone across Operating Systems.

Can anybody suggest a way around my problem?

Thanks,
Edd
 
M

Martin Dickopp

Edd said:
I have an array of strings containing filenames. I must open each in turn
and parse the data within. However, if a filename appears multiple times in
the list it must still only be read and parsed once.

Initially, to solve this problem I thought strcmp'ing the filenames would
suffice to check for duplicates in the list. However, it may be the case
that "somefile.in" identifies the same file as "~/inputs/somefile.in",
despite the strings being different. Thus simply using strcmp is inadequate.

Assuming:

FILE *fptr1 = fopen("somefile.in", "rb");
FILE *fptr2 = fopen("~/inputs/somefile.in", "rb");

both return non-NULL, is there a portable way of checking that fptr1 and
fptr2 are associated with the same stream?

No, not in ISO C. But many operating systems provide an OS specific
way to check this, so you could ask in a group dedicated to your OS
(e.g. comp.unix.programmer if you're writing code for Unix).
As a really nasty hack, I could check equality of the appropriate fields in
the FILEs. But that probably wouldn't port outside of a given implementation
of the standard library, let alone across Operating Systems.

I'd recommend to adhere to some standard that provides a way to achieve
what you want (e.g. POSIX if you're writing code for Unix) instead of
relying on implementation internals.

Martin
 
K

Keith Thompson

Edd said:
FILE *fptr1 = fopen("somefile.in", "rb");
FILE *fptr2 = fopen("~/inputs/somefile.in", "rb");
[...]

The C standard says very little about what file names are legal or how
they map to external files. In particular, the C standard doesn't say
anything about how the '~' and '/' characters in

"~/inputs/somefile.in"

might be interpreted (or the '.' character, for that matter).

If you're expecting '~' to have some particular special meaning, you
should be sure that that meaning applies to an fopen() call.

<OT>In Unix, it doesn't; if you have any questions about that, try
comp.unix.programmer.</OT>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top