Search code examples
c++cexceptionfopenfwrite

Debug Assertion Failed in fwrite.cpp


Sorry in advance for any lack of knowledge but im a C# developer attempting to look at a C problem.

A customer installed a piece of software on a new machine which is working on other machines without any issues.

They receive the error:

Debug Asertion Failed!

Program: C:\MyDirectory\MyDll.dll
File: minkernel\crts\ucrt\src\appcrt\studio\fwrite.cpp
Line: 33

Expression: stream != nullptr

The FOpen in question is being opened with w+ rights but is obviously failing which is why the FClose is throwing this exception. I know there should be a null check on the pointer before attempting an FClose but ill fix that later.

The FOpen just specifies the file name so should be created in the current working directory.

Because the FOpen is failing and the attempt is with w+ (which grants read, write and create file privilege), am i correct to assume that the only reason it can fail is due to invalid privileges at the operating system level for the program to create files?

I dont currently have access to the machine to have a look. Trying to accumulate possible issues before i get access.


Solution

  • A lot of things can make an fopen fail, so its hard to say. Insufficient privileges are, however, a quite common reason. To find out the real reason, you could use the error message provided by the operating system, which you can get as follows:

    #include <errno.h>
    ....
    char* filename = "example.txt";
    fp = fopen (filename,"w+");
    if (!fp) {
          fprintf(stderr, "error opening %s: %s", filename, strerror(errno));
          return;
    }
    

    Hope it helps :-)