C++中的常见I/O方式

59 篇文章 0 订阅
52 篇文章 2 订阅

目录

摘要

1. 标准输入输出(Standard I/O)

2. 文件输入输出(File I/O)

3. 字符串流(String Stream)

4. 低级文件I/O(Low-level File I/O)

5. 内存映射文件(Memory-Mapped File I/O)

6. 网络I/O(Network I/O)

服务器端

客户端


摘要

C++中的输入输出操作(I/O)方式多种多样,从简单的标准I/O到复杂的内存映射文件和网络I/O。每一种的用法都有其特别之处,下面会分别用代码的方式来简单介绍一些用法,可以根据自己项目所需来选择合适的I/O方式可以提高我们项目开发的效率。

1. 标准输入输出(Standard I/O)

标准输入输出用于与控制台进行交互。

#include <iostream>
#include <vector>
#include <string>
#include <sstream>
using namespace std;

struct Student {
    string name;
    int age;
    vector<int> grades;

    void display() const {
        cout << "Name: " << name << ", Age: " << age << ", Grades: ";
        for (int grade : grades) {
            cout << grade << " ";
        }
        cout << endl;
    }
};

int main() {
    vector<Student> students;
    string input;
    cout << "Enter student information (name age grades), type 'end' to stop:" << endl;
    while (true) {
        getline(cin, input);
        if (input == "end") break;

        istringstream iss(input);
        Student student;
        iss >> student.name >> student.age;
        int grade;
        while (iss >> grade) {
            student.grades.push_back(grade);
        }
        students.push_back(student);
    }

    cout << "Entered students:" << endl;
    for (const Student& student : students) {
        student.display();
    }

    return 0;
}

2. 文件输入输出(File I/O)

文件I/O操作允许将数据存储到文件中或从文件中读取。

#include <fstream>
#include <iostream>
#include <vector>
#include <string>
using namespace std;

struct Employee {
    string name;
    int id;
    double salary;

    void display() const {
        cout << "Name: " << name << ", ID: " << id << ", Salary: " << salary << endl;
    }
};

int main() {
    vector<Employee> employees = {
        {"Aoteman", 1, 50000},
        {"AotemanDaddy", 2, 60000},
        {"AotemanMami", 3, 70000}
    };

    ofstream outFile("employees.dat", ios::binary);
    if (!outFile) {
        cerr << "Error opening file for writing" << endl;
        return 1;
    }
    for (const Employee& emp : employees) {
        outFile.write((char*)&emp, sizeof(Employee));
    }
    outFile.close();

    vector<Employee> readEmployees;
    ifstream inFile("employees.dat", ios::binary);
    if (!inFile) {
        cerr << "Error opening file for reading" << endl;
        return 1;
    }
    Employee emp;
    while (inFile.read((char*)&emp, sizeof(Employee))) {
        readEmployees.push_back(emp);
    }
    inFile.close();

    cout << "Read employees:" << endl;
    for (const Employee& emp : readEmployees) {
        emp.display();
    }

    return 0;
}

3. 字符串流(String Stream)

字符串流用于处理内存中的字符串流。

#include <sstream>
#include <iostream>
#include <vector>
#include <string>
using namespace std;

struct Product {
    string name;
    double price;
    int quantity;

    void display() const {
        cout << "Name: " << name << ", Price: " << price << ", Quantity: " << quantity << endl;
    }
};

int main() {
    vector<Product> products = {
        {"Apple", 999.99, 5},
        {"HuaWei", 699.99, 10},
        {"SanXing", 299.99, 7}
    };

    ostringstream oss;
    for (const Product& prod : products) {
        oss << prod.name << " " << prod.price << " " << prod.quantity << "\n";
    }
    string productData = oss.str();
    cout << "Serialized products:\n" << productData;

    vector<Product> parsedProducts;
    istringstream iss(productData);
    string name;
    double price;
    int quantity;
    while (iss >> name >> price >> quantity) {
        parsedProducts.push_back({name, price, quantity});
    }

    cout << "Parsed products:" << endl;
    for (const Product& prod : parsedProducts) {
        prod.display();
    }

    return 0;
}

4. 低级文件I/O(Low-level File I/O)

低级文件I/O使用系统调用来进行文件操作,提供了更高的灵活性和控制力。

#include <fcntl.h>
#include <unistd.h>
#include <iostream>
#include <vector>
#include <cstring>
using namespace std;

struct Record {
    char name[20];
    int value;

    void display() const {
        cout << "Name: " << name << ", Value: " << value << endl;
    }
};

int main() {
    vector<Record> records = {
        {"Record1", 100},
        {"Record2", 200},
        {"Record3", 300}
    };

    int fd = open("records.dat", O_WRONLY | O_CREAT, 0644);
    if (fd == -1) {
        cerr << "Error opening file for writing" << endl;
        return 1;
    }
    for (const Record& rec : records) {
        write(fd, &rec, sizeof(Record));
    }
    close(fd);

    vector<Record> readRecords;
    fd = open("records.dat", O_RDONLY);
    if (fd == -1) {
        cerr << "Error opening file for reading" << endl;
        return 1;
    }
    Record rec;
    while (read(fd, &rec, sizeof(Record)) > 0) {
        readRecords.push_back(rec);
    }
    close(fd);

    cout << "Read records:" << endl;
    for (const Record& rec : readRecords) {
        rec.display();
    }

    return 0;
}

5. 内存映射文件(Memory-Mapped File I/O)

内存映射文件将文件内容映射到进程的地址空间。

#include <sys/mman.h>
#include <fcntl.h>
#include <unistd.h>
#include <iostream>
#include <cstring>
using namespace std;

struct Data {
    char text[50];
    int number;

    void display() const {
        cout << "Text: " << text << ", Number: " << number << endl;
    }
};

int main() {
    int fd = open("mmapdata.dat", O_RDWR | O_CREAT, 0644);
    if (fd == -1) {
        cerr << "Error opening file" << endl;
        return 1;
    }

    Data data = {"Memory-mapped file example", 42};
    write(fd, &data, sizeof(Data));

    size_t filesize = sizeof(Data);
    Data *map = static_cast<Data*>(mmap(0, filesize, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0));
    if (map == MAP_FAILED) {
        cerr << "Error mapping file" << endl;
        close(fd);
        return 1;
    }

    // 修改映射区域内容
    strcpy(map->text, "Updated text");
    map->number = 99;

    // 取消映射
    munmap(map, filesize);
    close(fd);

    // 读取文件内容
    fd = open("mmapdata.dat", O_RDONLY);
    if (fd == -1) {
        cerr << "Error opening file" << endl;
        return 1;
    }
    Data readData;
    read(fd, &readData, sizeof(Data));
    close(fd);

    cout << "Read data:" << endl;
    readData.display();

    return 0;
}

6. 网络I/O(Network I/O)

网络I/O用于通过网络进行数据传输,涉及到套接字编程。

服务器端

#include <sys/socket.h>
#include <netinet/in.h>
#include <unistd.h>
#include <iostream>
#include <cstring>
using namespace std;

int main() {
    int server_fd = socket(AF_INET, SOCK_STREAM, 0);
    if (server_fd == -1) {
        cerr << "Error creating socket" << endl;
        return 1;
    }

    struct sockaddr_in server_addr;
    server_addr.sin_family = AF_INET;
    server_addr.sin_port = htons(9090);
    server_addr.sin_addr.s_addr = INADDR_ANY;

    if (bind(server_fd, (struct sockaddr*)&server_addr, sizeof(server_addr)) == -1) {
        cerr << "Error binding socket" << endl;
        close(server_fd);
        return 1;
    }

    if (listen(server_fd, 5) == -1) {
        cerr << "Error listening on socket" << endl;
        close(server_fd);
        return 1;
    }

    cout << "Server listening on port 9090..." << endl;

    int client_fd = accept(server_fd, nullptr, nullptr);
    if (client_fd == -1) {
        cerr << "Error accepting connection" << endl;
        close(server_fd);
        return 1;
    }

    char buffer[256];
    ssize_t bytesReceived = recv(client_fd, buffer, sizeof(buffer) - 1, 0);
    if (bytesReceived == -1) {
        cerr << "Error receiving data" << endl;
        close(client_fd);
        close(server_fd);
        return 1;
    }
    buffer[bytesReceived] = '\0';
    cout << "Received from client: " << buffer << endl;

    const char *response = "Message received";
    send(client_fd, response, strlen(response), 0);

    close(client_fd);
    close(server_fd);

    return 0;
}

客户端

#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <iostream>
#include <cstring>
using namespace std;

int main() {
    int sockfd = socket(AF_INET, SOCK_STREAM, 0);
    if (sockfd == -1) {
        cerr << "Error creating socket" << endl;
        return 1;
    }

    struct sockaddr_in server_addr;
    server_addr.sin_family = AF_INET;
    server_addr.sin_port = htons(9090);
    server_addr.sin_addr.s_addr = inet_addr("127.0.0.1");

    if (connect(sockfd, (struct sockaddr*)&server_addr, sizeof(server_addr)) == -1) {
        cerr << "Error connecting to server" << endl;
        close(sockfd);
        return 1;
    }

    const char *msg = "Hello, server!";
    send(sockfd, msg, strlen(msg), 0);

    char buffer[256];
    ssize_t bytesReceived = recv(sockfd, buffer, sizeof(buffer) - 1, 0);
    if (bytesReceived == -1) {
        cerr << "Error receiving data" << endl;
    } else {
        buffer[bytesReceived] = '\0';
        cout << "Received from server: " << buffer << endl;
    }

    close(sockfd);
    return 0;
}

 一、ASCII 输出   为了使用下面的方法, 你必须包含头文件(译者注:在标准C++,已经使用取 代,所有的C++标准头文件都是无后缀的。)。这是 的一个扩展集, 提供有缓 冲的文件输入输出操作. 事实上, 已经被包含了, 所以你不必包含所有这两个 文件, 如果你想显式包含他们,那随便你。我们从文件操作类的设计开始, 我会讲解如何进行ASCII I/O 操作。如果你猜是"fstream," 恭喜你答对了! 但这篇文章介绍的方法,我们分别使用"ifstream"?和 "ofstream" 来作输入输出。   如果你用过标准控制台流"cin"?和 "cout," 那现在的事情对你来说很简单。 我们现在开始讲输出部 分,首先声明一个类对象。 ofstream fout;   这就可以了,不过你要打开一个文件的话, 必须像这样调用ofstream::open()。 fout.open("output.txt");   你也可以把文件名作为构造参数来打开一个文件. ofstream fout("output.txt");   这是我们使用的方法, 因为这样创建和打开一个文件看起来更简单. 顺便说一句, 如果你要打开的文 件不存在,它会为你创建一个, 所以不用担心文件创建的问题. 现在就输出到文件,看起来和"cout"的操 作很像。 对不了解控制台输出"cout"的人, 这里有个例子。 int num = 150; char name[] = "John Doe"; fout << "Here is a number: " << num << " "; fout << "Now here is a string: " << name << " ";   现在保存文件,你必须关闭文件,或者回写文件缓冲. 文件关闭之后就不能再操作了, 所以只有在你 不再操作这个文件的时候才调用它,它会自动保存文件。 回写缓冲区会在保持文件打开的情况下保存文 件, 所以只要有必要就使用它。回写看起来像另一次输出, 然后调用方法关闭。像这样: fout << flush; fout.close();    现在你用文本编辑器打开文件,内容看起来是这样:   Here is a number: 150 Now here is a string: John Doe   很简单吧! 现在继续文件输入, 需要一点技巧, 所以先确认你已经明白了流操作,对 "<>" 比较熟悉了, 因为你接下来还要用到他们。继续…   二、ASCII 输入   输入和"cin" 流很像. 和刚刚讨论的输出流很像, 但你要考虑几件事情。在我们开始复杂的内容之前 , 先看一个文本:   12 GameDev 15.45 L This is really awesome!   为了打开这个文件,你必须创建一个in-stream对象,?像这样。 ifstream fin("input.txt");   现在读入前四行. 你还记得怎么用"<<" 操作符往流里插入变量和符号吧?好,?在 "<>" (提取) 操作符. 使用方法是一样的. 看这个代码片段.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

沉夢志昂丶

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值