从C ++中的文本文件加载变量 [英] Loading Variables from Text File in C++

查看:88
本文介绍了从C ++中的文本文件加载变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这个问题已被问过一百万次,但大多数问题比我需要的更复杂。我已经知道哪些行将有什么数据,所以我只想加载每行作为自己的变量。

I know this question has been asked a million times before, but most of the questions are more complex than I need. I already know which lines will have what data, so I just want to load each line as its own variable.

例如,在settings.txt:



For example, in "settings.txt":

800
600
32

然后,在代码中,第1行设置为int winwidth,第2行设置为int winheight,第3行设置为int wincolor。

And then, in the code, line 1 is set to int winwidth, line 2 is set to int winheight, and line 3 is set to int wincolor.

对不起,我对I / O很新。

Sorry, I'm pretty new to I/O.

推荐答案

你可以这样做:

std::ifstream settings("settings.txt");
int winwidth;
int winheight;
int wincolor;

settings >> winwidth;
settings >> winheight;
settings >> wincolor;

但是,这不会确保每个变量都在一个新行,并且不包含任何错误处理。

However this will not ensure that each variable is on a new line, and doesn't contain any error handling.

这篇关于从C ++中的文本文件加载变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆