Bennu Game Development

English Forums => General => Topic started by: Sslaxx on June 20, 2011, 02:43:59 PM

Title: [1.0RC23] fopen - bug or intended behaviour?
Post by: Sslaxx on June 20, 2011, 02:43:59 PM
So, in my code I have a line like so:

a = fopen ("Test.ini", O_READWRITE);

If Test.ini doesn't exist fopens returns 0 and attempting file operations causes a segfault. If using O_READ or O_ZREAD that would be understandable; the file doesn't exist. O_WRITE works (for writing the file out) as it creates the file if it does not already exist. O_READWRITE, as just described, behaves in the same way as O_(Z)READ.

I'd have thought, given what it is, that O_READWRITE would have ensured the file would be created if it does not already exist. Is the fact it does not do so intended behaviour, or a bug?
Title: Re: [1.0RC23] fopen - bug or intended behaviour?
Post by: josebita on June 20, 2011, 03:50:53 PM
Not sure what the expected C equivalent behaviour is but as a general programming tip, it's always a good idea to ensure the file exists with file_exists() before trying to read from it.
Title: Re: [1.0RC23] fopen - bug or intended behaviour?
Post by: Sslaxx on June 20, 2011, 03:59:01 PM
If you go by this page (http://www.cplusplus.com/reference/clibrary/cstdio/fopen/), then you're saying O_READWRITE is functionally the same as "r+" - "Open a file for update both reading and writing. The file must exist." Makes sense.
Title: Re: [1.0RC23] fopen - bug or intended behaviour?
Post by: josebita on June 20, 2011, 04:31:08 PM
I just checked the BennuGD source code for mod_file and that's right, it's doing a "r+b" for O_READWRITE. (the "b" there is just a compatibility flag for old MS OSes)
Title: Re: [1.0RC23] fopen - bug or intended behaviour?
Post by: SplinterGU on June 20, 2011, 04:47:55 PM
no, no is a bug.

for rw operation the file must be exists, if fopen return 0, it mean that the fopen operation fail, you must check this return code before continue.