c++ - 在 C++ 中从文本文件加载变量

标签 c++

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

例如,在“settings.txt”中:

800
600
32

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

抱歉,我是 I/O 新手。

最佳答案

可能您可以做的最简单的事情是:

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

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

然而,这不能确保每个变量都在一个新行上,并且不包含任何错误处理。

关于c++ - 在 C++ 中从文本文件加载变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12208220/

相关文章:

java - 如何使用 C++ 读取用 Java (writeObject) 保存的文件

c++ - 如何使用 boost::bind 将返回 int 的函数转换为返回 bool 的函数?

c++ - 将 std::unique_ptr 插入 std::set

c++ - 代码块 16 位消息

c++ - C++ 字符串和字符串 vector 的 C 实现

c++ - Visual Studio 2013 如何检测缓冲区溢出

c++ - 使用 Spirit Qi 解析包含 std::string 的用户定义数据类型的 boost::variant

c++ - Unittest++ 不识别测试

c++ - 有没有办法将枚举类隐式转换为 std::byte?

c++ - 当列数固定时,Eigen::无法将 Block<Derived> 转换为 Ref<Derived>