#include <iostream>
#include <fstream>
// 根据不同平台选择不同的文件读写函数
#if defined(_WIN32)
// Windows 平台下的文件读写函数
void writeToFile(const std::string& filename, const std::string& content) {
std::ofstream file(filename, std::ios::binary);
if (file.is_open()) {
file.write(content.c_str(), content.size());
file.close();
} else {
std::cerr << "无法打开文件: " << filename << std::endl;
}
}
std::string readFromFile(const std::string& filename) {
std::ifstream file(filename, std::ios::binary);
std::string content;
if (file.is_open()) {
file.seekg(0, std::ios::end);
content.resize(file.tellg());
file.seekg(0, std::ios::beg);
file.read(&content[0], content.size());
file.close();
} else {
std::cerr << "无法打开文件: " << filename << std::endl;
}
return content;
}
#elif defined(__linux__)
// Linux 平台下的文件读写函数
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
void writeToFile(const std::string& filename, const std::string& content) {
int fd = open(filename.c_str(), O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR);
if (fd != -1) {
write(fd, content.c_str(), content.size());
close(fd);
} else {
std::cerr << "无法打开文件: " << filename << std::endl;
}
}
std::string readFromFile(const std::string& filename) {
int fd = open(filename.c_str(), O_RDONLY);
std::string content;
if (fd != -1) {
struct stat st;
fstat(fd, &st);
content.resize(st.st_size);
read(fd, &content[0], st.st_size);
close(fd);
} else {
std::cerr << "无法打开文件: " << filename << std::endl;
}
return content;
}
#else
// 其他平台的默认处理
#error "不支持的操作系统平台"
#endif
int main() {
std::string filename = "test.txt";
std::string content = "Hello, World!";
writeToFile(filename, content);
std::string readContent = readFromFile(filename);
std::cout << "读取的内容: " << readContent << std::endl;
return 0;
}
对代码可维护性的影响
- 优点:
- 清晰的平台区分:通过
#if defined
宏定义,不同平台的代码实现被明确区分开来,使得代码结构清晰,当需要修改某个平台特定的实现时,可以快速定位到对应的代码块,方便维护。
- 减少冗余:避免了在整个代码库中为每个平台重复编写通用的代码逻辑,只需要针对不同平台编写特定部分,减少了代码冗余,降低了维护成本。
- 缺点:
- 宏定义复杂:如果项目中有大量的平台特定代码和复杂的条件编译逻辑,宏定义可能会变得冗长和难以理解,增加了维护的难度。
对代码可移植性的影响
- 优点:
- 跨平台支持:这种做法允许在同一个代码库中支持多种操作系统平台,只需要根据不同平台的预定义宏选择相应的实现,大大提高了代码的可移植性。当需要将项目移植到新的平台时,只需要添加新的
#if defined
分支并编写对应的平台特定代码即可。
- 统一接口:对于不同平台的文件读写操作,对外提供了统一的函数接口
writeToFile
和readFromFile
,使得上层代码不需要关心底层平台的差异,提高了代码在不同平台间的可复用性。
- 缺点:
- 平台依赖性:虽然提高了可移植性,但代码仍然依赖于特定平台的预定义宏和底层系统调用,在某些情况下可能限制了代码在一些非标准或新兴平台上的直接移植。