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();
}
相关推荐
南山十一少1 小时前
Spring Security+JWT+Redis实现项目级前后端分离认证授权
java·spring·bootstrap
427724003 小时前
IDEA使用git不提示账号密码登录,而是输入token问题解决
java·git·intellij-idea
chengooooooo3 小时前
苍穹外卖day8 地址上传 用户下单 订单支付
java·服务器·数据库
李长渊哦3 小时前
常用的 JVM 参数:配置与优化指南
java·jvm
计算机小白一个3 小时前
蓝桥杯 Java B 组之设计 LRU 缓存
java·算法·蓝桥杯
黑不溜秋的4 小时前
C++ 设计模式 - 策略模式
c++·设计模式·策略模式
Dream it possible!6 小时前
LeetCode 热题 100_在排序数组中查找元素的第一个和最后一个位置(65_34_中等_C++)(二分查找)(一次二分查找+挨个搜索;两次二分查找)
c++·算法·leetcode
夏末秋也凉6 小时前
力扣-回溯-46 全排列
数据结构·算法·leetcode
南宫生6 小时前
力扣每日一题【算法学习day.132】
java·学习·算法·leetcode
柠石榴6 小时前
【练习】【回溯No.1】力扣 77. 组合
c++·算法·leetcode·回溯