c++写数据结构进入文件

以下定义一个数据结构

c 复制代码
struct SData {
	std::string url;
	int number;
	std::string memo;
};

写入文件

c 复制代码
void StorageDataToFile(const std::string& filename, const SData& data) {
   std::ofstream outFile(filename);
   if (outFile.is_open()) {
   	// 使用std::stringstream格式化数据
   	std::stringstream ss;
   	ss << data.url << "," << data.member << "," << data.memo << "\n";
   	// 将格式化后的字符串写入文件
   	outFile << ss.str();

   	outFile.close();
   	AfxMessageBox(L"ok storage") ;
   }
   else {
   	AfxMessageBox(L"error");
   }
}

读取

c 复制代码
UserData ReadDataFromFile(const std::string& filename) {
	SData data;
	std::ifstream inFile(filename);
	if (inFile.is_open()) {
		std::string line;
		getline(inFile, line);
		std::istringstream iss(line);
		if (iss >> data.url>> data.number >> data.memo) {
			inFile.close();
			return userData;
		}
		else {
			AfxMessageBox(L"error");
		}
	}
	else {
		AfxMessageBox(L"error");
	}

	// 如果读取失败,返回默认构造的UserData
	return UserData();
}
相关推荐
瑞雪兆丰年兮11 小时前
JavaWeb-day10-SpringBootWeb案例
java·开发语言·javaweb·web
Rain的Java大神之路12 小时前
你背的Spring答案,全是5.x时代的:7.0.8源码一翻,80%的博客都过时了
java·后端·面试
阿卡好可爱12 小时前
Lua 热更新不生效:改完文件线上仍是旧逻辑,package.loaded 与 upvalue 重绑定详解
数据结构·分布式·后端·架构·lua
沫璃染墨12 小时前
从零入门计算机网络系列(十八):TCP连接管理详解——从三次握手与四次挥手到TIME_WAIT机制
运维·服务器·网络·c++·网络协议·tcp/ip·tcp
这个昵称不容易12 小时前
考试报名系统的设计与实现(SpringBoot + MySQL + Vue 三角色平台)
java·springboot·毕设
Adellle13 小时前
设计模式指南 (Java 版) ㊁
java·开发语言·设计模式
特立独行的猫A13 小时前
cj-tauri:用仓颉写桌面与鸿蒙 PC 应用
java·后端·python
Lonely丶墨轩13 小时前
从一个 404 到负数 ID:第三方接口开发中的几次排错
java
自信1504130575913 小时前
重生之从0开始学习c++之——vector-中篇
c++·学习·算法
聪明的笨猪猪13 小时前
实际工程问题:AI生成MyBatis批量操作的SQL超限与性能雪崩
java·经验分享·面试·ai编程