Open two files at one time in C?

P

padh.ayo

Can C open (and keep open) two files at any given time?

so,

FILE *a, *b;

a = fopen(argv[1], "r");
b = fopen(argv[2], "r");

?
 
J

Joe Estock

Can C open (and keep open) two files at any given time?

so,

FILE *a, *b;

a = fopen(argv[1], "r");
b = fopen(argv[2], "r");

?

I don't know that it is defined in the C standard, however I've yet to
find an operating system that doesn't allow at least two files to be
open concurrently.
 
S

sgerchick

Yes you can. This is why you have
FILE *a, *b, ect

you can open as many as the enviornment allows
 
J

james of tucson

Can C open (and keep open) two files at any given time?

The number of open files is a platform-dependent quantity.

Some systems have this limit as a constant compiled into the OS kernel,
others define it with an environment variable, others have hard and soft
limits, allocated differently on a per-user basis, for instance, or a
total number of files that can be open system-wide versus open per-process.

One quite common implementation of the file descriptor in a stdio
library happens to use an 8-bit unsigned char (8 bits), which limits the
range of file descriptors which can be opened as FILE *'s to 0-255.

On the particular system I have in mind, descriptors 0,1, and 2 are
always reserved for stdin, stdout, and stderr respectively, leaving 253
fd's, which includes popen()'s and socket() and accept() calls also, and
so it is a very common real-world scenario that this limit is reached;
particularly in a network application. The 64-bit version of the OS I
am referring to, allows 65536 open files.
so,

FILE *a, *b;

a = fopen(argv[1], "r");
b = fopen(argv[2], "r");

?

I don't know of a system that has files at all, that doesn't allow you
to open at least two descriptors.
 
S

santosh

Can C open (and keep open) two files at any given time?

This is implementation defined and for a particular instance it is
given by the macro FOPEN_MAX in stdio.h. It includes stdin, stdout and
stderr.
 
J

jaysome

This is implementation defined and for a particular instance it is
given by the macro FOPEN_MAX in stdio.h. It includes stdin, stdout and
stderr.

And both C90 and C99 have this to say about FOPEN_MAX:

"The value of FOPEN_MAX shall be at least eight, including the three
standard text streams."

This means that the OP can indeed open (and keep open) two files at
any given time on any conforming C implementation.
 
R

Richard Bos

jaysome said:
And both C90 and C99 have this to say about FOPEN_MAX:

"The value of FOPEN_MAX shall be at least eight, including the three
standard text streams."

This means that the OP can indeed open (and keep open) two files at
any given time on any conforming C implementation.

Wrong. It means that _the implementation_ shall not stop you from
opening less than that. If the OS stops the implementation from opening
that many files for you, there's nothing to be done about it, at least
not within ISO C.
(What, for example, if strict sandboxing and rights restrictions mean
that there only is a single file which your program can even see, let
alone open?)

Richard
 
K

Keith Thompson

Wrong. It means that _the implementation_ shall not stop you from
opening less than that. If the OS stops the implementation from opening
that many files for you, there's nothing to be done about it, at least
not within ISO C.

The OS is part of the implementation. The implementation guarantees
that at least FOPEN_MAX files can be open simultaneously. If this
guarantee is not met, the implementation is non-conforming (though it
may not be the fault of, say, the compiler or the C runtime library).
(What, for example, if strict sandboxing and rights restrictions mean
that there only is a single file which your program can even see, let
alone open?)

What if the OS doesn't allow objects larger than 20,000 bytes? Then
the implementation is non-conforming.
 
R

Richard Bos

Keith Thompson said:
What if the OS doesn't allow objects larger than 20,000 bytes? Then
the implementation is non-conforming.

That would mean that an implementation can go from conforming to
non-conforming just because someone, possibly someone using a completely
different program, deletes readable file #8. In fact, an implementation
can go from non-conforming to conforming just by creating 8 files where
there were none; and go back to conforming if the program itself calls
remove() on one of those files. That cannot possibly be the intent.

Richard
 
C

Clever Monkey

Can C open (and keep open) two files at any given time?

so,

FILE *a, *b;

a = fopen(argv[1], "r");
b = fopen(argv[2], "r");

The question has been answered very well else-thread. However, did you
even try it?

Also, although this was mentioned in another reply, please consider that
most implementations give you three file descriptors upon program start.
 
C

Christopher Benson-Manica

That would mean that an implementation can go from conforming to
non-conforming just because someone, possibly someone using a completely
different program, deletes readable file #8. In fact, an implementation
can go from non-conforming to conforming just by creating 8 files where
there were none; and go back to conforming if the program itself calls
remove() on one of those files. That cannot possibly be the intent.

It seems to me that FOPEN_MAX is intended to guarantee that a call to
fopen() will not fail solely because of the number of files that are
already open until FOPEN_MAX files are opened. So as far as the OP is
concerned, it's reasonable to say that "If the implementation allows
you to open file A, and if the implementation allows you to open file
B, then the implementation must allow you to have file A and file B
open simultaneously." All the other details of what and how many files
a program may actually open are implementation-defined, are they not?
 
S

santosh

Clever said:
Can C open (and keep open) two files at any given time?

so,

FILE *a, *b;

a = fopen(argv[1], "r");
b = fopen(argv[2], "r");

The question has been answered very well else-thread. However, did you
even try it?

Also, although this was mentioned in another reply, please consider that
most implementations give you three file descriptors upon program start.

ITYM pointers to type FILE. As you know, file descriptors are an UNIX
specific lower level mechanism, and nothing to do with standard C.
 
C

Christopher Benson-Manica

Clever Monkey said:
Also, although this was mentioned in another reply, please consider that
most implementations give you three file descriptors upon program start.

If you're talking about stdin, stdout, and stderr, then all hosted
implementations must provide them, yes?
 
K

Keith Thompson

Christopher Benson-Manica said:
It seems to me that FOPEN_MAX is intended to guarantee that a call to
fopen() will not fail solely because of the number of files that are
already open until FOPEN_MAX files are opened. So as far as the OP is
concerned, it's reasonable to say that "If the implementation allows
you to open file A, and if the implementation allows you to open file
B, then the implementation must allow you to have file A and file B
open simultaneously." All the other details of what and how many files
a program may actually open are implementation-defined, are they not?

That makes excellent sense. Whether it follows from the wording of
the standard is another question, but one in which I'm afraid I've
suddenly lost interest, so I'll just go with your explanation.
 

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,769
Messages
2,569,577
Members
45,054
Latest member
LucyCarper

Latest Threads

Top