Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Open two files at one time in C?

Reply
Thread Tools

Open two files at one time in C?

 
 
padh.ayo@gmail.com
Guest
Posts: n/a
 
      11-30-2006
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");

?

 
Reply With Quote
 
 
 
 
Joe Estock
Guest
Posts: n/a
 
      11-30-2006
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> 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.
 
Reply With Quote
 
 
 
 
sgerchick@gmail.com
Guest
Posts: n/a
 
      11-30-2006
Yes you can. This is why you have
FILE *a, *b, ect

you can open as many as the enviornment allows

(E-Mail Removed) wrote:
> 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");
>
> ?


 
Reply With Quote
 
james of tucson
Guest
Posts: n/a
 
      11-30-2006
(E-Mail Removed) wrote:
> 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.
 
Reply With Quote
 
santosh
Guest
Posts: n/a
 
      11-30-2006
(E-Mail Removed) wrote:
> 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.

 
Reply With Quote
 
jaysome
Guest
Posts: n/a
 
      11-30-2006
On 29 Nov 2006 20:52:21 -0800, "santosh" <(E-Mail Removed)>
wrote:

>(E-Mail Removed) wrote:
>> 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.


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.

--
jay
 
Reply With Quote
 
Richard Bos
Guest
Posts: n/a
 
      11-30-2006
jaysome <(E-Mail Removed)> wrote:

> On 29 Nov 2006 20:52:21 -0800, "santosh" <(E-Mail Removed)>
> wrote:
>
> >(E-Mail Removed) wrote:
> >> 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.

>
> 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
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      11-30-2006
(E-Mail Removed) (Richard Bos) writes:
> jaysome <(E-Mail Removed)> wrote:
>> On 29 Nov 2006 20:52:21 -0800, "santosh" <(E-Mail Removed)>
>> wrote:
>>
>> >(E-Mail Removed) wrote:
>> >> 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.

>>
>> 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.


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.

--
Keith Thompson (The_Other_Keith) (E-Mail Removed) <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.
 
Reply With Quote
 
Richard Bos
Guest
Posts: n/a
 
      11-30-2006
Keith Thompson <(E-Mail Removed)> wrote:

> (E-Mail Removed) (Richard Bos) writes:
> > jaysome <(E-Mail Removed)> wrote:
> >> On 29 Nov 2006 20:52:21 -0800, "santosh" <(E-Mail Removed)>
> >> wrote:
> >>
> >> >(E-Mail Removed) wrote:
> >> >> 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.
> >>
> >> 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.


> > (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.


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
 
Reply With Quote
 
Clever Monkey
Guest
Posts: n/a
 
      11-30-2006
(E-Mail Removed) wrote:
> 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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to compare two SOAP Envelope or two Document or two XML files GenxLogic Java 3 12-06-2006 08:41 PM
Is time.time() < time.time() always true? flamesrock Python 8 11-24-2006 06:51 AM
Two unit 3750 stack: one cable or two? sphealey Cisco 6 06-30-2005 07:25 PM
Open All files one by one Billy Perl Misc 18 12-11-2004 05:14 PM
CSS aligning two things on one line with one left and one right news.frontiernet.net HTML 6 04-16-2004 02:44 AM



Advertisments