Re: CFile and FILE*
Thanks,
Using that link, this modification seems to work (although needs
development)
*********************************************************
#include "StdAfx.h"
#include <stdlib.h>
#include <stdio.h>
#include <fcntl.h>
#include <io.h>
#include "CrtFile.h"
FILE* CrtFile ( CFile* pCFile )
{
HFILE OsFileHandle;
FILE *stream;
int CrtFileHandle;
OsFileHandle = (HFILE) (pCFile ->m_hFile); // I added this ???
/* convert OS file handle to CRT file pointer */
if ( (CrtFileHandle=_open_osfhandle(OsFileHandle,_O_RDONLY))==-1){
printf( "_open_osfhandle Failed");
exit(1);
}
/* Change handle access to stream access. */
if( (stream = _fdopen( CrtFileHandle, "r" )) == NULL ) {
printf( "_fdopen Failed");
exit( 1 );
}
return stream;
}
void TestFile ()
{
CFile* pCFile = new CFile ( "test.txt", CFile::modeRead );
FILE* stream = CrtFile ( pCFile );
char inbuf[128];
int count = 0;
while( fgets( inbuf, 128, stream ) != NULL )
count++;
CString str;
str.Format ( "Lines=%d", count );
AfxMessageBox ( str );
}
*********************************************************
I needed this because I am writing data to an ancient device (firmware
unchangeable) which requires me to use the old compression algorithm
compress.c (which was written some time in the 1980's). As this code
uses every bad construct of the "C" language, I am loathe to try to
change it to C++.
"Victor" <nijegorodov.otpusk@freenet.de> wrote in message
news:#BoQApsjIHA.4076@TK2MSFTNGP05.phx.gbl:
Have a look at _open_osfhandle function and KB139640 ("Do Not Mix
Operating
System and CRT File Handles") in MSDN:
http://support.microsoft.com/kb/139640
Victor
"Ian Semmel" <anyone@rocketcomp.com.au> wrote in message
news:OtWxvmqjIHA.3512@TK2MSFTNGP03.phx.gbl...
Is there any way to call a function expecting a FILE* using an opened
CFile ?