首页 > 其他 > 详细

C 文件随机读写

时间:2017-05-06 22:49:37      阅读:240      评论:0      收藏:0      [点我收藏+]

技术分享
 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 
 4 void eatline(){
 5     while(getchar()!=\n)
 6         continue;
 7 }
 8 //文件随机读写
 9 int main(int argc, char * argv[])
10 {
11     FILE *fp;
12     int offset,tel;
13     long in_stream;
14     char ch,get;
15     long pos;
16     if((fp = fopen("E:/test/test.txt","r+b"))==NULL){
17         fp = fopen("E:/test/test.txt","w+b");
18         /*printf("Can‘t open this file.\n");
19         exit (1);*/
20     }
21     
22     while(1){
23         puts("Usage: number alpha, first input with an alpha will stop.");
24         int count = scanf("%d %c",&offset,&ch);
25         eatline();
26         if(count == 2){
27             //定位文件指针
28             fseek(fp,offset * sizeof(char),0);
29             //取出当前指针指向的数据,并保存指针偏移量
30             in_stream = fread(&get,sizeof(char),1,fp);
31             printf("char=%c\n",get);
32             //指针重新指向上一个索引处,并修改其中内容
33             fseek(fp,-in_stream*sizeof(char),1);
34             fwrite(&ch,sizeof(char),1,fp);
35         }else{
36             puts("Bye!");
37             fclose(fp);
38             return 0;
39         }
40     }
41     
42 }
C文件随机读写

C 文件随机读写

原文:http://www.cnblogs.com/endenvor/p/6818835.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!