Open two files at one time in C?

Discussion in 'C Programming' started by padh.ayo@gmail.com, Nov 30, 2006.

  1. Guest

    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");

    ?
    , Nov 30, 2006
    #1
    1. Advertising

  2. Joe Estock Guest

    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.
    Joe Estock, Nov 30, 2006
    #2
    1. Advertising

  3. Guest

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

    you can open as many as the enviornment allows

    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");
    >
    > ?
    , Nov 30, 2006
    #3
  4. 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.
    james of tucson, Nov 30, 2006
    #4
  5. santosh Guest

    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.
    santosh, Nov 30, 2006
    #5
  6. jaysome Guest

    On 29 Nov 2006 20:52:21 -0800, "santosh" <>
    wrote:

    > 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
    jaysome, Nov 30, 2006
    #6
  7. Richard Bos Guest

    jaysome <> wrote:

    > On 29 Nov 2006 20:52:21 -0800, "santosh" <>
    > wrote:
    >
    > > 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
    Richard Bos, Nov 30, 2006
    #7
  8. (Richard Bos) writes:
    > jaysome <> wrote:
    >> On 29 Nov 2006 20:52:21 -0800, "santosh" <>
    >> wrote:
    >>
    >> > 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) <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.
    Keith Thompson, Nov 30, 2006
    #8
  9. Richard Bos Guest

    Keith Thompson <> wrote:

    > (Richard Bos) writes:
    > > jaysome <> wrote:
    > >> On 29 Nov 2006 20:52:21 -0800, "santosh" <>
    > >> wrote:
    > >>
    > >> > 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
    Richard Bos, Nov 30, 2006
    #9
  10. 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.
    Clever Monkey, Nov 30, 2006
    #10
  11. Richard Bos <> wrote:

    > > (Richard Bos) writes:


    > > > (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?)


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

    --
    C. Benson Manica | I *should* know what I'm talking about - if I
    cbmanica(at)gmail.com | don't, I need to know. Flames welcome.
    Christopher Benson-Manica, Nov 30, 2006
    #11
  12. santosh Guest

    Clever Monkey wrote:
    > 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.


    ITYM pointers to type FILE. As you know, file descriptors are an UNIX
    specific lower level mechanism, and nothing to do with standard C.
    santosh, Nov 30, 2006
    #12
  13. Clever Monkey <> wrote:

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

    --
    C. Benson Manica | I *should* know what I'm talking about - if I
    cbmanica(at)gmail.com | don't, I need to know. Flames welcome.
    Christopher Benson-Manica, Nov 30, 2006
    #13
  14. Default User Guest

    Re: Open two files at one time in C? - TPA

    wrote:

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


    Please don't top-post. Your replies belong following or interspersed
    with properly trimmed quotes. See the majority of other posts in the
    newsgroup, or:
    <http://www.caliburn.nl/topposting.html>
    Default User, Nov 30, 2006
    #14
  15. Christopher Benson-Manica <> writes:
    > Richard Bos <> wrote:
    >> > (Richard Bos) writes:

    >
    >> > > (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?)

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


    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.

    --
    Keith Thompson (The_Other_Keith) <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.
    Keith Thompson, Nov 30, 2006
    #15
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. DC Gringo
    Replies:
    2
    Views:
    2,304
    Nikhil Patel
    Feb 26, 2004
  2. news.frontiernet.net
    Replies:
    6
    Views:
    1,121
    news.frontiernet.net
    Apr 16, 2004
  3. GenxLogic
    Replies:
    3
    Views:
    1,275
    andrewmcdonagh
    Dec 6, 2006
  4. flamesrock
    Replies:
    8
    Views:
    453
    Hendrik van Rooyen
    Nov 24, 2006
  5. Billy

    Open All files one by one

    Billy, Dec 10, 2004, in forum: Perl Misc
    Replies:
    18
    Views:
    229
    Billy
    Dec 11, 2004
Loading...

Share This Page