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;
}
相关推荐
电星托马斯几秒前
C++中顺序容器vector、list和deque的使用方法
linux·c语言·c++·windows·笔记·学习·程序人生
不知名。。。。。。。。7 分钟前
C++__list
开发语言·c++·list
仙人掌_lz20 分钟前
机器学习ML极简指南
人工智能·python·算法·机器学习·面试·强化学习
EverestVIP36 分钟前
C++动态库对外接口通过接口方式实现
开发语言·c++
Swift社区1 小时前
Swift LeetCode 246 题解:中心对称数(Strobogrammatic Number)
开发语言·leetcode·swift
巷北夜未央1 小时前
Python每日一题(13)
开发语言·python·算法
woniu_maggie1 小时前
SAP EXCEL DOI 详解
开发语言·后端·excel
小爬虫程序猿1 小时前
利用 PHP 爬虫按关键字搜索淘宝商品
开发语言·爬虫·php
独好紫罗兰1 小时前
洛谷题单3-P5720 【深基4.例4】一尺之棰-python-流程图重构
开发语言·python·算法
byte轻骑兵2 小时前
【C++进阶】关联容器:pair类型
开发语言·c++