- #include<fcntl.h>
- #include<iostream>
- #include<stdio.h>
- using namespace std;
- int main()
- {
- long a = 199;
- FILE* fd =fopen( "/root/YServeTest/cc","wb+" ); //二进制读写方式打开文件
- fwrite( &a , sizeof(long),1,fd );
- long ac = 88;
- fwrite( &ac , sizeof(long),1,fd );
- fseek ( fd, 0 , SEEK_SET); //重头读取
- while( true)
- {
- long b = -1;
- fread( &b ,sizeof(long), 1, fd ); //和read函数不同fread是不用不停lseek的
- if( feof( fd ) )
- break;
- cout<<b<<endl;
- }
- ftruncate(fileno( fd ), 0 ); //清空重头写入
- fseek ( fd, 0 , SEEK_SET); //文件读取位置初始化
- fwrite( &a , sizeof(long),1,fd );
- fclose( fd );
- }
- //该片段来自于http://www.codesnippet.cn/detail/0211201513938.html
来源: http://www.codesnippet.cn/detail/0211201513938.html