zoukankan      html  css  js  c++  java
  • C 文件操作1

    #include <stdio.h>
    #include <string.h>
    #define stuSize 1  // define的时候 没有数据类型 也不用写; 符号!!!
    void cpyFile(FILE *scource, FILE *dest);
    void writeFile(char *str); //将控制台的文字写入文件
    void getInfo();
    void saveToDisk(char path[32]);
    void checkFile(char path[32]);
    
    void main() {
        //将输入的信息写入文件
        writeFile("D:/abc.txt");
    
        //文件复制============================================
        FILE *fp1;
        FILE *fp2;
        char filename[10];  //若想用 char *s 的形式  注意要用malloc分配空间
    
        strcpy(filename, "D:/abc.txt");  // 变量名要写在前面 不能是 "xxx", filename
        fp1 = fopen(filename, "r");
    
        fp2 = fopen("D:/abc2.txt", "w");
        cpyFile(fp1, fp2);
    
        fclose(fp1);
        fclose(fp2);
    
        //写入二进制数据
        getInfo();
        char path[] = "D:/123123";
        saveToDisk(path);
        checkFile(path);
    
    }
    
    void writeFile(char *str) {
        FILE *fp;
        char ch; //本方法只能一个字一个字的写入文件  所以只能能定义成char
        char filename[10];
        strcpy(filename, str);
    
        if ((fp = fopen(filename, "w")) == NULL ) {
    
        } else { //getchar会将接收到的字符暂时放在缓冲区中  //回车后结束阻塞开始处理下面的语句 回车符本身也会放在缓冲区中
            while ((ch = getchar()) != '\n') {
                fputc(ch, fp);
            }
        }
        fclose(fp);
    
    }
    
    void cpyFile(FILE *scource, FILE *dest) {
    
        while (!feof(scource)) // 读取文件推荐用 feof() 作用是判断是否来到文件的末尾
        {
            fputc(fgetc(scource), dest);
        }
    }
    
    //使用fread fwrite 写入Strut类型数据=================================================
    
    struct Stu {
        char stuname[20];
        int no;
        int age;
        char addr[20];
    } stus[stuSize]; //结构体数组 推荐这么做 最好不要像Java那样在后面写Stu stus={ xxx,xx,xx,xx}
    
    void getInfo() {
        int i = 0;
        for (i = 0; i < 1; i++) {
            printf("input info 4:");
            scanf("%s", &stus[i].stuname); //scanf中只有一个要输入的参数//结束scanf的输入本身就是回车
            //若格式定义成这样  scanf("%s\n");  可能需要输入多次回车才有效果
            scanf("%d", &stus[i].no);
            scanf("%d", &stus[i].age);
            scanf("%s", &stus[i].addr);
        }
    }
    
    void saveToDisk(char path[32]) {
        //printf("%s",path ); //ok
        FILE *fp;
        if ((fp = fopen(path, "wb")) == NULL ) {  //wb write binary 写二进制文件
            printf("%s", "can nnot find file");
            return;
        }
        int i = 0;
        for (i = 0; i < stuSize; i++) {
            if (fwrite(&stus[i], sizeof(struct Stu), 1, fp) != 1) {
                printf("%s", "write error");
            }
        }
        fclose(fp);
    
    }
    
    void checkFile(char path[32]) {
        FILE *fp;
        if ((fp = fopen(path, "rb")) == NULL ) {
            printf("%s", "wrong!!");
        }
        int i = 0;
        for (i = 0; i < stuSize; i++) {
            fread(&stus[i], sizeof(struct Stu), 1, fp);
        }
        fclose(fp);//流用完了还是要关闭的 否则会出现读不到数据的情况
    
    }
  • 相关阅读:
    Python—Socket
    python-—计算器
    Python—I-O多路复用
    Python—redis
    《Python数据分析常用手册》一、NumPy和Pandas篇
    python--Selenium-模拟浏览器
    python--selenium简单模拟百度搜索点击器
    关于selenium实现滑块验证
    python 读写、创建 文件的方法(必看)
    Python 爬虫的工具列表大全
  • 原文地址:https://www.cnblogs.com/cart55free99/p/2978684.html
Copyright © 2011-2022 走看看