文件操作与文件流

文本文件:数据的存储方式为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)
原文地址:https://www.cnblogs.com/h-hkai/p/14792314.html