c++练习

1.将File练习题,内部的FILE*描述符,改成int描述符

cpp 复制代码
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <unistd.h>
#include <sstream>
#include <vector>
#include <memory>
#include <head.h>
using namespace std;
 
class File{
private:
	int fp;
public:
	File(){fp=open("./1.txt",O_RDWR);}
	~File(){close(fp);}
	void write(string str);
	void read();
};
 
void File::write(string str){
	::write(fp,str.data(),str.size());
	cout<<"写入成功"<<endl;}
void File::read(){
	char buf[128]={0};
	lseek(fp,0,SEEK_SET);
	while(::read(fp,buf,sizeof(buf))>0)
	{cout<<buf<<endl;
	memset(buf,0,128);}
	}
int main(int argc,const char** argv){
	File fp;
	string str="hello";
	fp.write(str);
	fp.read();
	return 0;
}

2。写一个类Fifo管道类。提高难度,什么都不提示。只要求:使用自己编写的Fifo类对象,实现2个终端之间互相聊天

cpp 复制代码
#include <iostream>
#include <fstream>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <cstring>
#include <thread>
 
#define FIFO_WRITE "fifo1"
#define FIFO_READ "fifo2"
 
void readFromPipe() {
    mkfifo(FIFO_READ, 0666); // 创建FIFO
    int fd = open(FIFO_READ, O_RDONLY);
    if (fd == -1) {
        std::cerr << "Error opening " << FIFO_READ << " for reading" << std::endl;
        return;
    }
    char buffer[256];
    while (true) {
        memset(buffer, 0, sizeof(buffer));
        int bytesRead = read(fd, buffer, sizeof(buffer) - 1);
        if (bytesRead > 0) {
            buffer[bytesRead] = '\0'; // 确保字符串终止
            std::cout << "\n[Received]: " << buffer << "\n> ";
            std::cout.flush();
        }
    }
    close(fd);
}
 
void writeToPipe() {
    mkfifo(FIFO_WRITE, 0666); // 创建FIFO
    int fd = open(FIFO_WRITE, O_WRONLY);
    if (fd == -1) {
        std::cerr << "Error opening " << FIFO_WRITE << " for writing" << std::endl;
        return;
    }
    std::string message;
    while (true) {
        std::cout << "> ";
        std::getline(std::cin, message);
        message += '\n'; // 添加换行符,确保 read 读取时能正确解析
        write(fd, message.c_str(), message.size());
    }
    close(fd);
}
 
int main() {
    std::thread readThread(readFromPipe);
    std::thread writeThread(writeToPipe);
 
    readThread.join();
    writeThread.join();
 
    return 0;
}
cpp 复制代码
#include <iostream>
#include <fstream>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <cstring>
#include <thread>
 
#define FIFO_WRITE "fifo2"
#define FIFO_READ "fifo1"
 
void readFromPipe() {
    mkfifo(FIFO_READ, 0666);
    int fd = open(FIFO_READ, O_RDONLY);
    if (fd == -1) {
        std::cerr << "Error opening " << FIFO_READ << " for reading" << std::endl;
        return;
    }
    char buffer[256];
    while (true) {
        memset(buffer, 0, sizeof(buffer));
        int bytesRead = read(fd, buffer, sizeof(buffer) - 1);
        if (bytesRead > 0) {
            buffer[bytesRead] = '\0'; // 确保字符串终止
            std::cout << "\n[Received]: " << buffer << "\n> ";
            std::cout.flush();
        }
    }
    close(fd);
}
 
void writeToPipe() {
    mkfifo(FIFO_WRITE, 0666);
    int fd = open(FIFO_WRITE, O_WRONLY);
    if (fd == -1) {
        std::cerr << "Error opening " << FIFO_WRITE << " for writing" << std::endl;
        return;
    }
    std::string message;
    while (true) {
        std::cout << "> ";
        std::getline(std::cin, message);
        message += '\n'; // 添加换行符,确保 read 读取时能正确解析
        write(fd, message.c_str(), message.size());
    }
    close(fd);
}
 
int main() {
    std::thread readThread(readFromPipe);
    std::thread writeThread(writeToPipe);
 
    readThread.join();
    writeThread.join();
 
    return 0;
}
相关推荐
科大饭桶26 分钟前
数据结构自学Day5--链表知识总结
数据结构·算法·leetcode·链表·c
一洽客服系统33 分钟前
网页嵌入与接入功能说明
开发语言·前端·javascript
mit6.8241 小时前
[Meetily后端框架] Whisper转录服务器 | 后端服务管理脚本
c++·人工智能·后端·python
zhangfeng11331 小时前
python 数据分析 单细胞测序数据分析 相关的图表,常见于肿瘤免疫微环境、细胞亚群功能研究 ,各图表类型及逻辑关系如下
开发语言·python·数据分析·医学
Sylvia-girl1 小时前
Java---IDEA
java·开发语言·intellij-idea
Z_W_H_1 小时前
【Springboot】Bean解释
java·开发语言
L_autinue_Star2 小时前
手写vector容器:C++模板实战指南(从0到1掌握泛型编程)
java·c语言·开发语言·c++·学习·stl
我爱C编程2 小时前
基于Qlearning强化学习的1DoF机械臂运动控制系统matlab仿真
算法
chao_7893 小时前
CSS表达式——下篇【selenium】
css·python·selenium·算法
元气小嘉3 小时前
前端技术小结
开发语言·前端·javascript·vue.js·人工智能