I'm attempting to use fopen
to open a file. I'm using an absolute path, the file does exist, and it's not corrupt. Also, all system users have full access to read/write the file.
const char* filename = "absolute\\path\\to\\file.something";
const char* mode = "rb";
FILE* file = fopen(filename, mode);
std::cout << "Errors: " << strerror(errno) << std::endl;
std::cout << "File errors: " << ferror(file) << std::endl;
perror(filename);
fclose(file);
All the error reporting shows no errors at all, yet fopen
returns a null pointer. If I try to seek the file I get an `Access Violation 0x00000014, which, from what I've read, just means the pointer is null. :)
Error reports:
strerror(errno)
: "No error"ferror(file)
: 0perror(filename)
: "No error"What other error checking could I possibly do? What's the next thing to try in order to figure this out?
To explain why I'm mixing C with C++: I'm going to be using a C library which needs a FILE handle, otherwise I would've used fstream. So, the hope is to get this fopen working so I can pass the FILE over to the C library. The rest of my project is in C++. :)
UPDATE #1:
This check does not work for me:
if (file == NULL) {
std::cout << "File is null" << std::endl;
}
This check does work for me:
if (file->_ptr == NULL) {
std::cout << "_ptr is null" << std::endl;
}
UPDATE #2:
This shows the FILE
as not NULL
:
std::cout << static_cast<void*>(file) << std::endl;
This shows the FILE
's pointer as 00000000
(NULL
):
std::cout << static_cast<void*>(file->_ptr) << std::endl;
UPDATE #3:
@chux has enlightened me. The file does indeed open correctly, as I just did a quick check and printed to the console the contents of the file and it didn't fail at all.
I was misinterpreting the file->_ptr
as showing NULL
to be the "true" value of the pointer. However, even though stepping through the code in the debugger shows it as NULL
, I was able to print the contents of the file - it was opened successfully all along!
Perhaps this question should be started again - perhaps elsewhere - due to the fact I now realise this is not an fopen
issue, but that of the library I'm passing the FILE
to. The library is experiencing the exception that originally brought my attention to checking if the file opened correctly, and eventually to seeing (in debugging) that the _ptr
shows as NULL
- which was misleading to me.
How embarrassing. :)
You need to save errno
before invoking any functions that may change it:
FILE* file = fopen(filename, mode);
int error_code = errno;
std::cout << "Errors: " << strerror(error_code) << std::endl;
In this particular code std::cout << "Errors: "
may change errno
.
OP's code returns a non-NULL
pointer per comment, indicating a successful fopen()
result.
Further OP states the file does exist
With no error in opening, no errors are expected using errno
, strerror(errno)
ferror(file)
.
The inner contents of *file
have no specification for code to use.
Best to not code if (file->_ptr == NULL) {
After some discussion here, it seems that I've misunderstood what's been happening.
In my case, there is no issue with opening the file. This is either an issue with the OggVorbis library (see original post: UPDATE: #3), or my approach to compiling the library, or both.
My apologies for any inconvenience caused by this irrelevant question, but I hope someone will also learn what I have learnt because of it:
file->_ptr
for NULL
, the FILE
itself will be NULL
if fopen
failsFILE
and throws an exception, check that the FILE
can be opened and its contents printed to the consoleThis is the post concerning my actual problem: OggVorbis ov_open() Throws Access Violation Exception
User contributions licensed under CC BY-SA 3.0