当前位置: 首页 > news >正文

C++练习

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

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

file.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;
}

fifo1.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;
}

fifo2.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;
}

相关文章:

  • C# dataGridView 自动生成几行几列及手动输入整型字符
  • nginx https配置
  • 【算法】并查集基础讲解
  • 每日c/c++题 备战蓝桥杯(全排列问题)
  • DEEPSEEK创业项目推荐:
  • pytorch中不同的mask方法:masked_fill, masked_select, masked_scatter
  • MySQL 当中的锁
  • 网络运维学习笔记(DeepSeek优化版)026 OSPF vlink(Virtual Link,虚链路)配置详解
  • 深度学习 Deep Learning 第13章 线性因子模型
  • PyQt6实例_批量下载pdf工具_批量pdf网址获取
  • 3.30学习总结 Java包装类+高精度算法+查找算法
  • 开发环境解决Secure Cookie导致302重定向
  • VUE实现框架搭建(纯手写)
  • 【Python爬虫神器】requests库常用操作详解 ,附实战案例
  • RocketMQ - 从消息可靠传输谈高可用
  • Cookie可以存哪些指?
  • 一区严选!挑战5天一篇脂质体组学 DAY1-5
  • Flink介绍——实时计算核心论文之S4论文详解
  • RS232转Profinet网关扫码器在西门子1200plc快速配置
  • MySQL中的CREATE TABLE LIKE和CREATE TABLE SELECT
  • 做高档衣服的网站/上海优化价格
  • 洛阳做家教去什么网站/百度引流推广怎么收费
  • 下载app安装/朝阳seo搜索引擎
  • 双公示网站专栏建设情况/百度极速版免费下载安装
  • 银川商城网站开发设计/牛奶软文广告营销
  • 深圳市营销型网站建设/互联网广告行业分析