Problem writing int to binary file in C

前端 未结 6 2180
一整个雨季
一整个雨季 2021-01-06 21:11

I need to write data to a binary file using C\'s I/O functions. The following code causes a runtime exception :


#include \"stdio.h\"

int main(int argc,char         


        
6条回答
  •  情书的邮戳
    2021-01-06 22:11

     fwrite((const void*)val,sizeof(int),1,fp);
    

    should be:

     fwrite((const void*) & val,sizeof(int),1,fp);
    

    BTW, if you don't use the cast, you will get a sensible error message. Casts tend to be used by C (and C++) programmers far more often than they should be - a good rule of thumb is "if it needs a cast, it's probably wrong".

提交回复
热议问题