angus
angus

Reputation: 2367

Set binary mode after file is opened

My function

void myFunction (FILE *f);

gets an already opened file. I need to write a literal CR+LF, so I want to set f's mode to binary.

How can I do that?

Upvotes: 4

Views: 555

Answers (1)

Roger Rowland
Roger Rowland

Reputation: 26279

As per comments, perhaps a function such as the following may be useful (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 might conditionally #define the function name instead, but I don't think it's ever going to be pretty.

Upvotes: 2

Related Questions