Example/* FOPEN.C: This program opens files named "data" * and "data2".It uses fclose to close "data" and * _fcloseall to close all remaining files. */#include <stdio.h>FILE *stream, *stream2;void main( void ) { int numclosed; /* Open for read (will fail if file "data" does not exist) */ if( (stream = fopen( "data", "r" )) == NULL ) printf( "The file 'data' was not opened\n" ); else printf( "The file 'data' was opened\n" ); /* Open for write */ if( (stream2 = fopen( "data2", "w+" )) == NULL ) printf( "The file 'data2' was not opened\n" ); else printf( "The file 'data2' was opened\n" ); /* Close stream */ if( fclose( stream ) ) printf( "The file 'data' was not closed\n" ); /* All other files are closed: */ numclosed = _fcloseall( ); printf( "Number of files closed by _fcloseall: %u\n", numclosed ); } OutputThe file 'data' was opened The file 'data2' was opened Number of files closed by _fcloseall: 1
if ( fp == NULL )
{
fp = fopen( W2A( strFileName ),"rb");
if ( fp == NULL )
{
return ;
}
} LocalLog* pLog = new LocalLog;
memset(pLog, 0, sizeof(LocalLog));
int nCount = 0;
while ( fread(pLog, sizeof(LocalLog),1,fp) != NULL )
{
nCount++;
FillQueryInfo( *pLog, nCount);
}
delete pLog;
fflush(fp);
fclose(fp);
这个是已数据结构来读的,你也可以改成一行一行来读
* and "data2".It uses fclose to close "data" and
* _fcloseall to close all remaining files.
*/#include <stdio.h>FILE *stream, *stream2;void main( void )
{
int numclosed; /* Open for read (will fail if file "data" does not exist) */
if( (stream = fopen( "data", "r" )) == NULL )
printf( "The file 'data' was not opened\n" );
else
printf( "The file 'data' was opened\n" ); /* Open for write */
if( (stream2 = fopen( "data2", "w+" )) == NULL )
printf( "The file 'data2' was not opened\n" );
else
printf( "The file 'data2' was opened\n" ); /* Close stream */
if( fclose( stream ) )
printf( "The file 'data' was not closed\n" ); /* All other files are closed: */
numclosed = _fcloseall( );
printf( "Number of files closed by _fcloseall: %u\n", numclosed );
}
OutputThe file 'data' was opened
The file 'data2' was opened
Number of files closed by _fcloseall: 1
从TXT里得到的数据存在哪个变量里了?
CStdioFile::ReadString()
{
AfxMessageBox( _T("读文件出错:\n" ) + SReadTxtFilePathName );
// return;
}// fileR.SeekToBegin (); while(fileR.ReadString(SReadStr))
{ //SReadStr 就是每一行的内容。 }
fileR.Close();
GetCurrentDirectory(256,path);//应用程序所在路径
CString filepath = path;
filepath += "\\data.txt";//你的文件的绝对路径,假设你的文件名是data.txt CStdioFile m_file(filepath,CFile::modeRead);
CString contents;
while (m_file.ReadString(contents))//逐行读取文本内容
{
//contents中保存的就是"data.txt"文件中每一行的内容.可以使用了
}