5

Is there a way to tell what mode a FILE * was opened?

Specifically I need to tell if a file stream is writable. Either just the true/false result of whether it's writable, or the const char * of the mode itself is good.

1
  • 2
    There is no standard way to do it, but most platforms will let you do it in a way or another. If you're not looking to be fully cross-platform (or don't mind having different code for different platforms), you should specify your OS and you will possibly get a decent answer.
    – zneak
    Aug 31, 2013 at 2:30

4 Answers 4

3

Not directly answer your question, but you can use freopen

FILE *freopen(const char *path, const char *mode, FILE *stream);

It opens a specified file on a specified stream, closing the stream first if it is already open. If the stream previously had an orientation, freopen clears it.

3

YES, there are ways but they are historical in nature and frowned upon by others. Also, you will need to research / test what the exact values are and then take the risk that a new release of the OS will undo your assumptions.

Anyway, given that caveat, you can test fields inside the FILE structure for specific values. The structure is in stdio.h and the fields of interest are: _flag and _file.

Write-up some characterization code and run on your system, as:

#include <stdio.h>

main()
{
    FILE *fp;

    fp = fopen("test1.txt", "r");
    printf("(r) _flag = %04x, _file = %04x\n", fp->_flag, fp->_file);

    fp = fopen("test2.txt", "a");
    printf("(a) _flag = %04x, _file = %04x\n", fp->_flag, fp->_file);

    fp = fopen("test3.txt", "w");
    printf("(w) _flag = %04x, _file = %04x\n", fp->_flag, fp->_file);

}

You can see the _flag values associated with each type of open.

On Windows/XP, I got the following:

(r) _flag = 0001, _file = 0003
(a) _flag = 0002, _file = 0004
(w) _flag = 0002, _file = 0005

Notice, append is the same as write! The _file values appear to be the index into the _iob, where stdin is _file = 0000.

Anyway, mess around, experiment. Try writing/reading data from the files and see if _flag changes???

5
  • Is this based on any standard, or just Windows? Aug 31, 2013 at 3:17
  • It's origins pre-dates K&R, second edition. It is my understanding that Unix keeps a more abbreviate version of the FILE structure. But you should be able to find in stdio.h and see if FILE contains a _flag or _mode field in Unix. I use Windows/XP and can get that info. Aug 31, 2013 at 3:54
  • 1
    Any implementation has such a thing, so it's a vacuous truth to say its origins predate a standard. K&R second edition was essentially a commentary on the C90 standard, IIRC, but not an attempt at standardization nor innovation. Unix is neither a standard nor an operating system. This answer is nothing more or less than a suggestion to reverse-engineer whatever platform you are on, which is not a good suggestion. Aug 31, 2013 at 4:24
  • 1
    @Potatoswatter This does not work with the glibc: it declares the FILE type as a truly opaque type; all that stdio.h says is that there is some struct _IO_FILE, but there is no definition for it. So, it should be said, that this is a 100% hack if you find a way to do this. Aug 31, 2013 at 9:05
  • @cmaster, rather, code written for Windows (where I do know it is possible) is NOT directly portable to *nix (where I would have to figure out what to do). Note, to determine format of _IO_FILE, code something like: printf("%0xf", *((char *) fp)); then fp+4, etc. and puzzle it out. Aug 31, 2013 at 15:41
2

In general, no there is not. You will need to keep track of this information yourself. You may be able to attempt writing to the file and either get an error or success, but that depends on exactly what you're trying to do.

0
FILE * f;
//...
if (f->flags == 2 || f->flags == 3) {
     // writable
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.