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;
}
相关推荐
JANYI20184 分钟前
在c++中老是碰到string&,这是什么意思?
开发语言·c++
S01d13r24 分钟前
LeetCode 解题思路 45(分割等和子集、最长有效括号)
算法·leetcode·职场和发展
passionSnail1 小时前
《MATLAB实战训练营:从入门到工业级应用》趣味入门篇-用声音合成玩音乐:MATLAB电子琴制作(超级趣味实践版)
开发语言·matlab
锦夏挽秋1 小时前
Qt 信号槽机制底层原理学习
c++·qt
shenyan~1 小时前
关于Python:9. 深入理解Python运行机制
开发语言·python
天堂的恶魔9461 小时前
C++ - 仿 RabbitMQ 实现消息队列(1)(环境搭建)
开发语言·c++·rabbitmq
殇淋狱陌2 小时前
【Python】常用命令提示符
开发语言·python·虚拟环境
anqi272 小时前
在sheel中运行Spark
大数据·开发语言·分布式·后端·spark
理想奋斗中2 小时前
【LeetCode Hot100 | 每日刷题】二叉树的层序遍历
算法·leetcode·bfs
VB.Net3 小时前
C# 综合示例 库存管理系统20 操作员管理(FormAdmin)
开发语言·数据库·c#