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