文件输出

mz126 mz126 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
求大神知道 为什么10之前的数无法正常打出

/* 将 1 - 100 写入文件 然后倒序读出来 */

#include <stdio.h>
#include <stdlib.h>
int main( void )
{
FILE* pfile = NULL;
int val = 0;
long cnt = 0L;
int i = 0;

// open the file
pfile = fopen( "abc.txt" , "w" );
if( !pfile )
{
fprintf( stderr , "can not open the file \n" );
exit( 1 );
}

// write the file
for( val = 1 ; val <= 100 ; ++ val )
{
fwrite( &val , sizeof( int ) , 1 , pfile );
}

// close the file
fclose( pfile );

// open the file
pfile = fopen( "abc.txt" , "r" );

for( i = 1 ; i <= 100 ; ++ i )
{
fseek( pfile , - i * sizeof( int ) , SEEK_END );
fread( &val , sizeof( int ) , 1 , pfile );
printf( "%d " , val );
if( i % 10 == 0 ) printf( "\n" );
}

// close the file
fclose( pfile );

return 0;
}
...全文
评论 7
收藏 已收藏
分享
返回顶部
mz126
C语言 70,039 成员 243,237 内容