locked
_setmode visual studio 2008 C++ RRS feed

  • Question

  • I am trying to use _setmode in my project to convert stdin and stdout into binary format. But the compiler keep getting an error:

    error C3861: '_setmode': identifier not found.

    I am using visual studio 2008 and coding in C++ language.

    Below is the macro i am using:

    #if defined (MSDOS) || defined(OS2) || defined(WIN32) || defined(_CYGWIN_)

    #include <fcntl.h>

    #include<io.h>

    #define SET_BINARY_MODE(file) _setmode(_fileno(file), _O_BINARY)

    #else

    #define SET_BINARY_MODE(file)

    #endif

    I also tried running the sample code given in the msdn library but it produce the same error again.

    sample code: http://msdn.microsoft.com/en-us/library/tw4k6df8(v=vs.90).aspx

    Please help. Much appreciated

    Wednesday, July 25, 2012 3:11 AM

Answers

  • Ok. I solved the problem already. It seems visual studio have 2 copies of io.h. 1 in the VC\include folder and the other one in VC\crt\src.

    #define <io.h> link it to the first one in the "VC\include" folder which does not have the _setmode function.

    I change it to:

    #define "c:\Program Files\Microsoft Visual Studio 9.0\VC\crt\src\io.h" and it works.

    • Marked as answer by Elegentin Xie Tuesday, July 31, 2012 7:14 AM
    Wednesday, July 25, 2012 5:40 AM

All replies

  • What happens when you comment out:

    #if defined (MSDOS) || defined(OS2) || defined(WIN32) || defined(_CYGWIN_)

    Also, by any chance is _POSIX_ defined anywhere? That would prevent the declaration of _setmode().

    Wednesday, July 25, 2012 3:19 AM
  • if i comment out

    #if defined (MSDOS) || defined(OS2) || defined(WIN32) || defined(_CYGWIN_)

    and

    #else

    #define SET_BINARY_MODE(file)

    it still produce the same error. I don't think i define POSIX any where. Cause I tried creating a new project using only the sample code from MSDN forum and it still doesn't work.

    BTW the project I am trying to compile is the zlib compression sample. "zpipe.c"

    Wednesday, July 25, 2012 3:23 AM
  • Ok. I solved the problem already. It seems visual studio have 2 copies of io.h. 1 in the VC\include folder and the other one in VC\crt\src.

    #define <io.h> link it to the first one in the "VC\include" folder which does not have the _setmode function.

    I change it to:

    #define "c:\Program Files\Microsoft Visual Studio 9.0\VC\crt\src\io.h" and it works.

    • Marked as answer by Elegentin Xie Tuesday, July 31, 2012 7:14 AM
    Wednesday, July 25, 2012 5:40 AM
  • The following code is working fine. 

    #include "stdafx.h"
    #if defined (MSDOS) || defined(OS2) || defined(WIN32) || defined(_CYGWIN_)
    #include <fcntl.h>
    #include<io.h>
    #define SET_BINARY_MODE(file) _setmode(_fileno(file), _O_BINARY)
    #else
    #define SET_BINARY_MODE(file)
    #endif
    
    
    
    int _tmain(int argc, _TCHAR* argv[])
    {
    
    	int result;
       result = SET_BINARY_MODE(  stdin ) ;
       if( result == -1 )
          perror( "Cannot set mode" );
       else
          printf( "'stdin' successfully changed to binary mode\n" );
    
    
    	return 0;
    }


    Thanks and Regards Selvam http://www15.brinkster.com/selvamselvam/

    Wednesday, July 25, 2012 12:31 PM
  • >#define <io.h> link it to the first one in the "VC\include"
    >folder which does not have the _setmode function.

    I trust you actually meant #include <io.h> here.

    >I am using visual studio 2008

    Odd. My copy of VC++ 2008 Express (no SP) has _setmode
    in the VC\include\io.h file.

    - Wayne
    • Proposed as answer by Elegentin Xie Tuesday, July 31, 2012 7:14 AM
    Wednesday, July 25, 2012 12:47 PM