• 文件操作与文件流


    文本文件:数据的存储方式为ASCII码形式

    二进制文件:数据的存储方式为二进制(01)形式

    fopen&fwrite

    #include <stdio.h>
    #include <string.h>
    
    #include <iostream>
    
    using namespace std;
    
    int main() {
        char str[100];
        cin >> str;
        FILE *fp = fopen("a.txt", "w");
        if (fp == NULL) {
            cout << "cann't open the file" << endl;
            return 0;
        } else {
            fwrite(str, sizeof(char), strlen(str), fp);
            fclose(fp);
        }
    
        return 0;
    }

    fread&fwrite

    #include <stdio.h>
    #include <string.h>
    
    #include <iostream>
    
    using namespace std;
    
    int main() {
        const char *filename = "a.txt";
        FILE *fp = fopen(filename, "r+");  // 把文件名用指针传入
        if (fp == NULL) {
            cout << "cann't open the file" << endl;
            return 0;
        }
    
        char buf[200];
        int n = fread(buf, 1, 128, fp);  // 指向文件数据的指针指向文件的末尾
        buf[n] = '';
        cout << "the data in file is following" << endl << buf << endl;
        for (int i = 0; i < n; ++i) {  // 完成大小写字母的转换
            if (buf[i] >= 'a' && buf[i] <= 'z') {
                buf[i] = buf[i] - 32;
            } else if (buf[i] >= 'A' && buf[i] <= 'Z') {
                buf[i] = buf[i] + 32;
            }
        }
        cout << "the changed data is: " << buf << endl;
       //rewind(fp);  // 把指向文件的指针指向文件的开始位置,如果在进行写入的话相当于对文件进行重写 fwrite(buf,
    1, n, fp); fclose(fp); return 0; }

    文件流

    用对象打开文件

    ifstream infile;        // 定义文件流

    infile.open("filename.txt");     // 对文件流进行操作

    #include <fstream>
    #include <iostream>
    
    using namespace std;
    
    int main() {
        char filename1[256], filename2[256];
        char buf[300];
        cout << "Input sourec file name: ";
        cin >> filename1;
        cout << "Input destination: ";
        cin >> filename2;
        fstream infile, outfile;
        infile.open(filename1, ios::in);    // 读入已经存在的文件
        outfile.open(filename2, ios::out);  // 打开要写入的文件
        while (infile.getline(buf, 300))   // 当读到文件末尾时会返回0
        outfile << buf << endl; outfile.close(); infile.close(); return 0; }

    write

    #include <fstream>
    #include <iostream>
    
    using namespace std;
    
    struct Stu {
        char name[20];
        int num;
        int age;
        char sex;
    };
    
    int main() {
        Stu stud[3] = {"Tom", 1, 18, 'm', "Jerry", 2, 17, 'f', "Phil", 3, 20, 'm'};  // 结构体初始化
        ofstream outfile("sutd.dat", ios::binary);
        if (!outfile) {
            cout << "Open error!" << endl;
            abort();  // 终止程序对内存不做清理
        }
        for (int i = 0; i < 3; ++i) outfile.write((char*)&stud[i], sizeof(stud[i]));  // 将引用强制转换为指针
        outfile.close();
        return 0;
    }

    read

    #include <fstream>
    #include <iostream>
    
    using namespace std;
    
    struct Stu {
        char name[20];
        int num;
        int age;
        char sex;
    };
    
    int main() {
        Stu stud[3];
        ifstream infile("sutd.dat", ios::binary);
        if (!infile) {
            cerr << "open error!" << endl;
            abort();
        }
        for (int i = 0; i < 3; ++i) {
            infile.read((char*)&stud[i], sizeof(stud[i]));
        }
        infile.close();
        for (int i = 0; i < 3; ++i) {
            cout << "No." << i + 1 << endl;
            cout << "name: " << stud[i].name << endl;
            cout << "num: " << stud[i].num << endl;
            cout << "age: " << stud[i].age << endl;
            cout << "sex: " << stud[i].sex << endl;
        }
    }
    永远渴望,大智若愚(stay hungry, stay foolish)
  • 相关阅读:
    最详解JavaScript常见的创建对象的七种方式(推荐)
    详解数组的迭代方法every()、filter()、forEach()、map()以及some()的用法
    详解数组的concat()、slice()、splice()方法
    解决微信小程序中web-view无法调用微信支付
    SQLAlchemy_定义(一对一/一对多/多对多)关系
    常用算法
    Python框架之Tornado (源码之褪去模板外衣)
    Python框架之Tornado(源码之褪去模板外衣的前戏)
    Python框架之Tornado(请求)
    Python框架之Tornado(请求阶段)
  • 原文地址:https://www.cnblogs.com/h-hkai/p/14792314.html
Copyright © 2020-2023  润新知