字符串和输入文件,从包含字符串的txt文件中指定多个变量值 我对C++编程很陌生,我不确定输入和输出流的工作原理。我想使用一个txt文件来分配类成员变量,该文件包含由分隔符(“:”)分隔的字符串和字符

字符串和输入文件,从包含字符串的txt文件中指定多个变量值 我对C++编程很陌生,我不确定输入和输出流的工作原理。我想使用一个txt文件来分配类成员变量,该文件包含由分隔符(“:”)分隔的字符串和字符,c++,string,file,variables,input,C++,String,File,Variables,Input,到目前为止,我已经能够有一个txt文件,其中每行包含一个双精度或整数,并将它们分配给变量。目前我拥有: 当前的“Default.txt” 十, 三, 0.80 1.5 我当前用于变量赋值的代码: #include <iostream> #include <fstream> RandomObject::RandomObject(){ //The Default Constructor std::ifstream my_file; my_file.open("

到目前为止,我已经能够有一个txt文件,其中每行包含一个双精度或整数,并将它们分配给变量。目前我拥有:

当前的“Default.txt” 十,

三,

0.80

1.5

我当前用于变量赋值的代码:

#include <iostream>
#include <fstream>

RandomObject::RandomObject(){ //The Default Constructor

std::ifstream my_file;

my_file.open("Default.txt");

//Not sure if this the most effective way to do this...
    if (my_file.is_open())
    {
        while (~my_file.eof())
        {   my_file >> Var1;   //Var1 is now an int with value 10
            my_file >> Var2;   //Var2 is now an int with value 3
            my_file >> Var3;   //Var3 is now double with value 0.80
            my_file >> Var4;   //Var4 is now double with value 1.5
            break;
        }
    
    }
    my_file.close();
}
和一些输出文本,指示变量初始化:

“变量1设置为10”

“每个维度的数量为10”

我该怎么做呢?txt文件将以相同的顺序包含所有值,但如果可能的话,让程序读取“每个维度的编号”作为第一个字符串自动将10分配给Var1可能会很有用

我猜第一步是把它分成两个字符串,用“:”作为分隔符,然后将它存储在某个临时字符串数组变量中?我不确定读取这行代码并将值存储在上面的代码中到底发生了什么


我想了解文件流实际上是如何工作的。很抱歉,格式不正确。

请尝试类似以下内容:

#include <iostream>
#include <string>
#include <fstream>
#include <sstream>

RandomObject::RandomObject() //The Default Constructor
{
    std::ifstream my_file("Default.txt");
    std::string line;

    while (std::getline(my_file, line))
    {
        std::istringstream iss(line);
        std::string name;

        std::getline(iss, name, ':');

        if (name == "Number Per Dimension") {
            iss >> Var1;   //Var1 is now an int with value 10
            std::cout << name << " is " << Var1 << std::endl;
        }

        else if (name == "Number of Dimensions") {
            iss >> Var2;   //Var2 is now an int with value 3
            std::cout << name << " is " << Var2 << std::endl;
        }

        else if (name == "Number Density") {
            iss >> Var3;   //Var3 is now double with value 0.80
            std::cout << name << " is " << Var3 << std::endl;
        }

        else if (name == "Initial Temperature") {
            iss >> Var4;   //Var4 is now double with value 1.5
            std::cout << name << " is " << Var4 << std::endl;
        }
    }
}
#包括
#包括
#包括
#包括
RandomObject::RandomObject()//默认构造函数
{
std::ifstream my_文件(“Default.txt”);
std::字符串行;
while(std::getline(我的_文件,第行))
{
标准::istringstream iss(线);
std::字符串名;
std::getline(iss,名称“:”);
如果(名称=“每个维度的编号”){
iss>>Var1;//Var1现在是一个值为10的int

std::cout我不会给你写一些代码,因为你有上面的代码,所以我将向你解释我将如何做/以及这些代码是如何工作的,并给你一些有用的链接

使用
std::getline(文件名,行)
读取一行。
getline()
有一个默认分隔符
\n
。因此它将整行存储在变量
行中

getline()
返回的内容存储在字符串变量中后,用于查找
“:”
及其位置。它将返回一个
大小\u t位置
,与和/或函数一起使用以获取不同变量中的这两个部分

字符串变量
String var_text
与您的文本将保持原样,但由于您希望将其设置为
int
double
的值需要修改。您必须首先将
String var_text
与所需的任何字符串进行比较。根据结果,您可以将变量设置为
int
double

现在您已经拥有了所需格式的变量,可以将其存储在适当的类成员中

重复此操作,只要
getline()
返回1。

使用
而(!my_file.eof())
而不是
~
,这是一种二进制反转,不是逻辑反转。在
循环时,不要在
中使用
my_file.eof()
#include <iostream>
#include <string>
#include <fstream>
#include <sstream>

RandomObject::RandomObject() //The Default Constructor
{
    std::ifstream my_file("Default.txt");
    std::string line;

    while (std::getline(my_file, line))
    {
        std::istringstream iss(line);
        std::string name;

        std::getline(iss, name, ':');

        if (name == "Number Per Dimension") {
            iss >> Var1;   //Var1 is now an int with value 10
            std::cout << name << " is " << Var1 << std::endl;
        }

        else if (name == "Number of Dimensions") {
            iss >> Var2;   //Var2 is now an int with value 3
            std::cout << name << " is " << Var2 << std::endl;
        }

        else if (name == "Number Density") {
            iss >> Var3;   //Var3 is now double with value 0.80
            std::cout << name << " is " << Var3 << std::endl;
        }

        else if (name == "Initial Temperature") {
            iss >> Var4;   //Var4 is now double with value 1.5
            std::cout << name << " is " << Var4 << std::endl;
        }
    }
}