473,386 Members | 1,790 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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

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

Nov 14 '05 #1
4 1193
"Edd" <ed***********@nunswithguns.net> writes:
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
--
,--. Martin Dickopp, Dresden, Germany ,= ,-_-. =.
/ ,- ) http://www.zero-based.org/ ((_/)o o(\_))
\ `-' `-'(. .)`-'
`-. Debian, a variant of the GNU operating system. \_/
Nov 14 '05 #2
Martin Dickopp wrote:

"Edd" <ed***********@nunswithguns.net> writes:
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).


Check out (in support of Martin's post) ->

http://david.tribble.com/text/c0xfstat.html
Stephen
Nov 14 '05 #3
Edd
Stephen L. wrote:
Martin Dickopp wrote:

"Edd" <ed***********@nunswithguns.net> writes:
[ 8< - - - snip ]
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).


Check out (in support of Martin's post) ->

http://david.tribble.com/text/c0xfstat.html


Ah, that's pretty handy! I'll probably be coding my project in a *nix
environment, but I'd like it to work on windows too. The code found at the
end of the link you posted should make things considerably easier -- thanks!

Edd
Nov 14 '05 #4
"Edd" <ed***********@nunswithguns.net> writes:
[...]
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>

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Mario | last post by:
Hello, I couldn't find a solution to the following problem (tried google and dejanews), maybe I'm using the wrong keywords? Is there a way to open a file (a linux fifo pipe actually) in...
6
by: radnoraj | last post by:
Hi, I am sucessfull in redirecting console output to a file. but in this case nothing is displayed on the console, cout output is written to file without display. how do write the output to...
2
by: Chris | last post by:
Hi, What is the most easy way to check on EOF while reading a binary file with all integers ? In lot of examples the read data are first stored in a string, and afterwards the string is...
1
by: lkr | last post by:
hi i got one file transfer program using serialization which has a limitation that i can send only 8192 bytes(8KB). i want to send more than that wht can i do. how can i divide the file into...
7
by: PJ | last post by:
I have an open FileStream and I open a BinaryWriter on it. After writing to this file, I want to read it to another stream. FileStream fs = new FileStream(Path.GetTempFileName()); BinaryWriter...
5
by: IcingDeath via DotNetMonster.com | last post by:
I am building this SQL Server database app in which i can store files. In order to display files I want to have the app show the associated icon for the extension of the file that is in the...
14
by: prasadjoshi124 | last post by:
Hi All, I am writing a small tool which is supposed to fill the filesystem to a specified percent. For, that I need to read how much the file system is full in percent, like the output given...
1
AdrianH
by: AdrianH | last post by:
Assumptions I am assuming that you know or are capable of looking up the functions I am to describe here and have some remedial understanding of C programming. FYI Although I have called this...
27
by: Jason | last post by:
Hi, I need to open an existing file, seek to a position at X number of bytes, and write out Y number of bytes overwriting any existing bytes, but no erasing any other data. Is this possible? ...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.