check for read or write

This is a discussion on check for read or write within the C Programming forums, part of the General Programming Boards category; I use Code: int fildes; fildes = open("try.txt", O_RDONLY); to open a file "try.txt" for read. How do i check ...

  1. #1
    Registered User
    Join Date
    Feb 2005
    Posts
    30

    check for read or write

    I use
    Code:
    int fildes;
    fildes = open("try.txt", O_RDONLY);
    to open a file "try.txt" for read. How do i check that this file is open for read or write later on?

  2. #2
    Registered User
    Join Date
    Sep 2004
    Posts
    719
    i believe you need to use a file pointer rather than an int - it's been so long since i've done c though.

    but, if an interger is correct, then i suspsect that the easiest way to do it is
    Code:
    int fildes = 0;
    fildes = open("try.txt", O_RDONLY);
    
    if(fildes)
        printf("File is open\n");
    
    i seem to have GCC 3.3.4
    But how do i start it?
    I dont have a menu for it or anything.

  3. #3
    Registered User
    Join Date
    Sep 2004
    Posts
    719
    ..also you might want to specify fildes to be constant for various subtle reasons.
    but, you would then have to do
    const int fildes = open("try.txt", O_RDONLY);
    i seem to have GCC 3.3.4
    But how do i start it?
    I dont have a menu for it or anything.

  4. #4
    Registered User
    Join Date
    Feb 2005
    Posts
    30
    Hi..
    Actually what i am asking is how to check that "try.txt" is open for read or write, that means some functions will tell you that "try.txt" is open for read, so i cannot perform any write function; or "try.txt" is open for write, so i cannot perform any read function.

  5. #5
    ---
    Join Date
    May 2004
    Posts
    1,379
    I don't think you can. if you need to read and write then open it it that way. If you need to read from it first then write to it later then close it and re-open it.

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,422
    Why aren't you using fopen() etc?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  7. #7
    Registered User
    Join Date
    Sep 2004
    Posts
    719
    edit: the following are opinions and educated guesses. you are responsible for checking it's accuracy

    i don't think there are predefined functions. it sounds rather silly to open a file and not know it's write permissions.

    however, if you must, you can always hack it by writing a byte to the file, and if write returns 1 then you canwrite to the file. now just erase the byte. if it returns 0, then you can't write to it. this is not necessarily because you don't have write permissions. it could mean that the file is not open or some other error has occured. but nonetheless, it means you can't write to it (at least at the time the write was called). you can also do pretty much the same for read. now, if you use the 2 return values to decide it's exact permissions.

    eq
    if(read && write)
    its read/write
    if(read && !write)
    it's read only
    if(!read && write)
    it's write only
    i seem to have GCC 3.3.4
    But how do i start it?
    I dont have a menu for it or anything.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. fread problems or memory problems
    By Lechuza in forum C Programming
    Replies: 1
    Last Post: 03-22-2009, 12:45 PM
  2. MFC check box question
    By MyglyMP2 in forum Windows Programming
    Replies: 2
    Last Post: 03-09-2009, 05:47 PM
  3. Why is fread sometimes taking so long?
    By manugarciac in forum C++ Programming
    Replies: 2
    Last Post: 04-28-2007, 11:25 PM
  4. Weird problem with fwrite() and fread()
    By piote in forum C Programming
    Replies: 2
    Last Post: 11-13-2004, 02:07 PM
  5. Game Programming FAQ
    By TechWins in forum Game Programming
    Replies: 5
    Last Post: 09-29-2004, 02:00 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21