概述
文件可以存储任何非结构化字节序列,这个比较简单,就一个写一个读;学习到此,留个记录,以后可以直接抄代码,哈哈
Demo代码
#include <fstream>
#include <iostream>
#include <thread>
#include <vector>
#include <unistd.h>
std::string kSharedFile = "/tmp/test.bin";
template<class T>
class Writer {
private:
std::ofstream out;
public:
Writer(std::string& name):
out(name, std::ofstream::binary) {}
void Write(const T& data) {
out.write(reinterpret_cast<const char*>(&data), sizeof(T));
}
};
template<class T>
class Reader {
private:
std::ifstream in;
public:
Reader(std::string& name) {
for(int count=10; count && !in.is_open(); count--) {
in.open(name, std::ifstream::binary);
std::this_thread::sleep_for(std::chrono::milliseconds(10));
}
}
T Read() {
int count = 10;
for (;count && in.eof(); count--) {
std::this_thread::sleep_for(std::chrono::milliseconds(10));
}
T data;
in.read(reinterpret_cast<char*>(&data), sizeof(data));
if (!in) {
throw std::runtime_error("Failed to read a message");
}
return data;
}
};
struct Message {
int x, y;
};
std::ostream& operator<<(std::ostream& o, const Message& m) {
o << "(x=" << m.x << ", y=" << m.y << ")";
}
void DoWrites() {
std::vector<Message> messages {{1, 0}, {0, 1}, {1, 1}, {0, 0}};
Writer<Message> writer(kSharedFile);
for (const auto& m : messages) {
std::cout << "Write " << m << std::endl;
writer.Write(m);
}
}
void DoReads() {
Reader<Message> reader(kSharedFile);
try {
while(true) {
std::cout << "Read " << reader.Read() << std::endl;
}
} catch (const std::runtime_error& e) {
std::cout << e.what() << std::endl;
}
}
int main(int argc, char** argv) {
if (fork()) {
DoWrites();
} else {
DoReads();
}
}
我公司承接各类技术服务,主要聚焦于:stm32、单片机、嵌入式、QT应用开发、Web+Python+Django应用开发。欢迎合作。