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 }