Set binary mode after opening a file

My function

void myFunction (FILE *f);

gets an open file. I need to write a literal CR+LF, so I want to set the mode fto binary.

How can i do this?

+4
source share
1 answer

According to the comments, perhaps a feature may be useful, such as the following (untested!):

#include <stdio.h>

#ifdef WIN32
#include <fcntl.h>
#include <io.h>
#endif

int SetBinary(FILE *pFile)
{
    // set file mode to binary
#ifdef WIN32
    return _setmode(_fileno(pFile), O_BINARY);
#else
    return setmode(_fileno(pFile), O_BINARY);
#endif
}

It looks ugly, so maybe you could conditionally #defineinstead of a function name, but I don’t think that someday it will be beautiful.

+2
source

All Articles