社区
C++ 语言
帖子详情
如何通过文件流将一个结构体写入文件或者从文件中读取信息填充结构体?
tsaiyc
2007-04-17 05:43:18
一个结构体,说简单点
struct person
{
char name[20];
bool sex;
}
//填充person结构体
。。。
//填充完毕
ifstream input("person.txt", ios::in);
本来想用input<<(void*)&person;
发现编译错误。
//读取部分略,同样问题。错误是<<右边类型出错。
因此求助。
...全文
397
5
打赏
收藏
如何通过文件流将一个结构体写入文件或者从文件中读取信息填充结构体?
一个结构体,说简单点 struct person { char name[20]; bool sex; } //填充person结构体 。。。 //填充完毕 ifstream input("person.txt", ios::in); 本来想用input<<(void*)&person; 发现编译错误。 //读取部分略,同样问题。错误是<<右边类型出错。 因此求助。
复制链接
扫一扫
分享
转发到动态
举报
写回复
配置赞助广告
用AI写文章
5 条
回复
切换为时间正序
请发表友善的回复…
发表回复
打赏红包
zwlzwlzwl
2007-04-17
打赏
举报
回复
ofstream input("person.txt", ios::in);
input<<person.name<<person.sex;
FingerStyle
2007-04-17
打赏
举报
回复
自己再完善下吧。。。
FingerStyle
2007-04-17
打赏
举报
回复
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
struct person
{
char name[20];
bool sex;
person()
{
strcpy(name, "classpatterns");
sex = 1;
}
};
template <class T>
class m_stream_iter
{
public:
ostream * ou;
m_stream_iter(ostream& t)
{
ou = &t;
}
m_stream_iter<T>& operator= (T& p)
{
for (int i=0; i<20; i++)
{
*ou << p.name[i];
}
*ou << p.sex << endl;
return *this;
}
m_stream_iter<T>& operator * ()
{
return *this;
}
m_stream_iter<T>& operator ++()
{
return *this;
}
};
int main()
{
ofstream out("a.txt");
vector<person> pV;
person a;
pV.push_back(a);
copy(pV.begin(), pV.end(), m_stream_iter<person>(out));
return 0;
}
freshui
2007-04-17
打赏
举报
回复
person 不是 built-in 类型的缘故?
roger_77
2007-04-17
打赏
举报
回复
同一个问题。
TemplateInputException异常
当发现TemplateInputException异常很多时候是你这里可能出现了问题 两种解决方案: 报错分析: 1、controller层请求处理结束后返回数据时,没有使用类注解@RestController或注解@ResponseBody而返回了非json格式的数据。 2、返回的数据thymeleaf模板不能解析,所以报错。 灵感来源于https://blog.csdn.net/libusi001/article/details/100162576 ...
关于Thymeleaf框架的TemplateInputException解决方法
关于Thymeleaf框架的TemplateInputException解决方法 问题描述 经常在后端代码没有出现问题的情况下,前端加载Thymeleaf框架时出现prg.thymeleaf.exceptions.TemplateInputException: An error happened during template parsing (template: "class path resource [templates/xxx.html]")的问题。顺便记录一下能够实现此动态加载方式的思路。 思路分
Stringboot异常TemplateInputException
Stringboot异常TemplateInputException stringboot查询列表时出现TemplateInputException异常 解决方法 加入@ResonseBody注解即可解决。 原因 错误显示template might not exist or might not be accessible by any of the configured Template Resolvers(模板可能不存在,也可能无法被任何配置的模板解析器访问 ) 没有ResponseBody,底
错误org.thymeleaf.exceptions.TemplateInputException解决
在运行
一个
controller时报错,controller代码如下: @Controller @RequestMapping(value = "/goods") public class GoodsController { @Autowired private GoodsService goodsService; @RequestMapping(value = "/fi...
报错org.thymeleaf.exceptions.TemplateInputException: Error resolving template解决方案
最近在使用springboot+thymeleaf写程序的时候,一直报错org.thymeleaf.exceptions.TemplateInputException: Error resolving template硬是搞了整整一天才解决,因为自己踩过坑了,所以,在这里,本人给出目前我所搜集到的所有报这个错的情况的解决方案。 org.thymeleaf.exceptions.Templat
C++ 语言
65,213
社区成员
250,514
社区内容
发帖
与我相关
我的任务
C++ 语言
C++ 语言相关问题讨论,技术干货分享,前沿动态等
复制链接
扫一扫
分享
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++
技术论坛(原bbs)
社区管理员
加入社区
获取链接或二维码
近7日
近30日
至今
加载中
查看更多榜单
社区公告
请不要发布与C++技术无关的贴子
请不要发布与技术无关的招聘、广告的帖子
请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下
试试用AI创作助手写篇文章吧
+ 用AI写文章