zoukankan      html  css  js  c++  java
  • C 文件随机读写

    
    
     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++汇编器、连接器
    【转载】vi的使用命令
    JDK,SDK,JRE概念
    iOS 使用xmpp做聊天客户端
    cocopods安装
    用XMPP实现完整Android聊天项目
    xmpp发送文件
    ember.js学习笔记
    html5 drag and drop
    jquery 数组深拷贝
  • 原文地址:https://www.cnblogs.com/endenvor/p/6818835.html
Copyright © 2011-2022 走看看