欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 新闻 > 资讯 > c++练习

c++练习

2025/4/4 12:35:04 来源:https://blog.csdn.net/mNinGInG/article/details/146798840  浏览:    关键词:c++练习

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

#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个终端之间互相聊天

#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); // 创建FIFOint 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); // 创建FIFOint 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;
}

#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;
}

 

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词