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;
}
相关推荐
PingdiGuo_guo3 分钟前
C++数据类型、变量常量
开发语言·c++
Ethan Hunt丶6 分钟前
MSVTNet: 基于多尺度视觉Transformer的运动想象EEG分类模型
人工智能·深度学习·算法·transformer·脑机接口
仟濹22 分钟前
【算法打卡day10(2026-02-24 周二)复习算法:DFS BFS 并查集】
算法·深度优先·图论·dfs·bfs·广度优先·宽度优先
水饺编程24 分钟前
第4章,[标签 Win32] :TextOut 测试案例3代码改编
c语言·c++·windows·visual studio
-海绵东东-28 分钟前
哈希表······················
算法·leetcode·散列表
Darkwanderor34 分钟前
数据结构 - 并查集的应用
数据结构·c++·并查集
LuDvei37 分钟前
LINUX文件操作函数
java·linux·算法
多恩Stone1 小时前
【C++ debug】在 VS Code 中无 Attach 调试 Python 调用的 C++ 扩展
开发语言·c++·python
PingdiGuo_guo1 小时前
C++联合体详解!
开发语言·c++
XW01059991 小时前
4-11判断素数
前端·python·算法·素数