C++ 中用什么类来代替C中fopen等函数
编号:QA001038
建立日期: 1999年5月25日 最后修改日期:1999年12月5日
所属类别:
Bill Herz:
操作系统:Windows 95 OSR2.1
编程语言:Visual C++ 6
遇到问题:请问在 VC++ 中用什么类来代替C中的 fopen、fclose、fscanf 等函数?
回答:
张云潮的意见:
你仍然可以在VC++ 中使用C语言的标准文件函数。
在VC++ 中 CFile类可以完成C中对文件操作函数的功能。CFile 是MFC对文件操作的总司令, 有许多值得研究的地方。以下是CFile对文件的一些基本操作:
char* pszFileName = "c:\\test\\myfile.dat";
CFile myFile;
char szBuffer[256];
UINT nActual = 0;
CFileException fileException;
if ( !myFile.Open( pszFileName, CFile::modeCreate |
CFile::modeReadWrite ), &fileException )
{
TRACE( "Can't open file %s, error = %u\n",
pszFileName, fileException.m_cause );
}
myFile.Write( szBuffer, sizeof( szBuffer ) );
myFile.Seek( 0, CFile::begin );
nActual = myFile.Read( szBuffer, sizeof( szBuffer ) );
myFile.Close();
Cancel的意见:
Just use the class CStdioFile. It is a sub class of CFile, for detail please refer to MSDN->CStdioFile
此问题由张云潮等回答。
| |
|
|
| |
|
|