Chicko Bueno
Chicko Bueno

Reputation: 337

C++: fread() causing assertion error?

I have an error cause by fread() and it says:

File: f:\ dd\vctools\ crt_bld\ self_x86\ crt\ src\fread.c
Line: 102
Expression: (stream != NULL)
For information on how your program can cause an assertion
failure, see the Visual C++ documentation on asserts.
(Press Retry to debug the application)

What should I do? I have already include <stdio.h> in my program. I'm currently developing my project using Visual C++ MFC.

Upvotes: 0

Views: 4864

Answers (4)

Bo Persson
Bo Persson

Reputation: 92381

Have you tried the (Press Retry to debug the application) part of the message.

If you enter the debugger, you will likely end up at the assert() and can check the call stack to find where the call to fread comes from. Then figure out why the FILE* is null there! Perhaps a failed call to fopen?

Upvotes: 1

shoosh
shoosh

Reputation: 79021

The function fread() takes as the an argument a FILE*:

size_t __cdecl fread(
    void *buffer,
    size_t elementSize,
    size_t count,
    FILE *stream
)

The FILE* you are sending it is NULL. you should debug your program and find out why that is so.

You could have found this information on your on, the same way I did, by opening the file f:\ dd\vctools\ crt_bld\ self_x86\ crt\ src\fread.c in your computer and looking what it does on line 102.

Upvotes: 1

harald
harald

Reputation: 6126

Have you verified that you have a proper FILE object to send to fread? Try to check if it is null before using it. Also check out that documentation on assert, it's not about what files you have #included or not. This is a runtime error.

Upvotes: 1

Bertrand Marron
Bertrand Marron

Reputation: 22220

Expression: (stream != NULL) explains it all.

You're passing a NULL stream to fread and you should not.

Upvotes: 6

Related Questions