关于FILE操作的简单问题 我想创建一个二进制文件(用文本编辑器看不到其中的内容),保存部分数据。然后打开该文件,读取出需要的数据。谁有代码参考一下,谢谢! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 大概如此://example for CFile::CFilechar* pFileName = "test.dat";TRY{ CFile f( pFileName, CFile::modeCreate | CFile::modeWrite|CFile::typeBinary );}CATCH( CFileException, e ){ #ifdef _DEBUG afxDump << "File could not be opened " << e->m_cause << "\n"; #endif}END_CATCH 然后:写数据:char pbuf[100];cfile.Write( pbuf, 100 );读数据:char pbuf[100];UINT nBytesRead = cfile.Read( pbuf, 100 );关闭:cfile.Flush(); cfile.close(); Example/* FREAD.C: 本程序以二进制方式打开 FREAD.OUT,写入25个整数,然后再打开FREAD.OUT 并读入25个整数。如果成功,会显示所读的整数个数。 */#include <stdio.h>void main( void ){ FILE *stream; int list[30]; int i, numread, numwritten; /* Open file in text mode: */ if( (stream = fopen( "fread.out", "w+b" )) != NULL ) { for ( i = 0; i < 25; i++ ) list[i] = i; numwritten = fwrite( list, sizeof( int ), 25, stream ); printf( "Wrote %d items\n", numwritten ); fclose( stream ); } else printf( "Problem opening the file\n" ); if( (stream = fopen( "fread.out", "r+b" )) != NULL ) { /* Attempt to read in 25 characters */ numread = fread( list, sizeof( int), 25, stream ); printf( "Number of items read = %d\n", numread ); fclose( stream ); for (i=0; i < 25; i++) printf("%d\n",list[i]); } else printf( "File could not be opened\n" );}OutputWrote 25 itemsNumber of items read = 25Contents of buffer = zyxwvutsrqponmlkjihgfedcb 漂亮进度条制作的问题.现需要制作一个比较特殊的Progress,如何给进度条套一个自己指定的漂亮皮肤,请达人指点。 手头上有用IBM T4X的本本的朋友帮个忙好吗? 如何在两个进程中共享一个链表? 请问用SQL怎样查询特定的记录??? 那位大侠能提供ActiveSkin做的xp风格的界面皮肤。可令加100分。谢谢。 为什么不到我的键盘钩子里来那?? CHtmlView中取源代码 怎么读取ACCESS数据库里的OLE对象?(ADO) 有听说过神达公司的么? 紧急求助(系统为何禁止注册ocx) 问一个软件!!! ===如何获得小于等于200的double类型的随机数(带小数)?===
char* pFileName = "test.dat";
TRY
{
CFile f( pFileName, CFile::modeCreate | CFile::modeWrite|CFile::typeBinary );
}
CATCH( CFileException, e )
{
#ifdef _DEBUG
afxDump << "File could not be opened " << e->m_cause << "\n";
#endif
}
END_CATCH
写数据:
char pbuf[100];
cfile.Write( pbuf, 100 );
读数据:
char pbuf[100];
UINT nBytesRead = cfile.Read( pbuf, 100 );
关闭:
cfile.Flush();
cfile.close();
*/#include <stdio.h>void main( void )
{
FILE *stream;
int list[30];
int i, numread, numwritten; /* Open file in text mode: */
if( (stream = fopen( "fread.out", "w+b" )) != NULL )
{
for ( i = 0; i < 25; i++ )
list[i] = i;
numwritten = fwrite( list, sizeof( int ), 25, stream );
printf( "Wrote %d items\n", numwritten );
fclose( stream ); }
else
printf( "Problem opening the file\n" ); if( (stream = fopen( "fread.out", "r+b" )) != NULL )
{
/* Attempt to read in 25 characters */
numread = fread( list, sizeof( int), 25, stream );
printf( "Number of items read = %d\n", numread );
fclose( stream );
for (i=0; i < 25; i++) printf("%d\n",list[i]);
}
else
printf( "File could not be opened\n" );
}
OutputWrote 25 items
Number of items read = 25
Contents of buffer = zyxwvutsrqponmlkjihgfedcb