简体   繁体   English

字符串和输入文件,从包含字符串的txt文件中分配几个变量值

[英]Strings and input files, Assign several variables values from txt file that contains strings

I'm new to programming in C++ and I'm not sure how exactly the input and output stream work. 我是C ++编程的新手,我不确定输入和输出流的工作方式。 I want to take a txt file, which contains strings and characters that are separated by a delimiter (': '), to assign class member variables. 我想获取一个txt文件,该文件包含由定界符(':')分隔的字符串和字符,以分配类成员变量。

So far I been able to have a txt file that contains a single double or integer per line and assigned them to variables. 到目前为止,我已经能够拥有一个txt文件,该文件每行包含一个双精度或整数,并将它们分配给变量。 Currently what I have: 目前我有:

Current "Default.txt" 当前的“ Default.txt”

10 10

3 3

0.80 0.80

1.5 1.5

The code that I'm currently using for variable assignment: 我当前用于变量分配的代码:

#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();
}

Now I want the txt file to be: 现在我希望txt文件为:

The new "Default.txt" 新的“ Default.txt”

Number Per Dimension: 10 每维数:10

Number of Dimensions: 3 尺寸数:3

Number Density: 0.80 密度:0.80

Initial Temperature: 1.5 初始温度:1.5

I want the value after the colon to be assigned to a member variable: 我希望将冒号后的值分配给成员变量:

int Var1 = 10;
int Var2 = 3;
double Var3 = 0.80;
double Var4 = 1.5;

and some output text indicating variable intialization of either: 和一些输出文本,指示其中一个的变量初始化:

"Variable 1 is set to 10" “变量1设置为10”

or 要么

"Number Per Dimension is 10". “每维数为10”。

How do I go about doing this? 我该怎么做呢? The txt file will have all the values in the same order, although it might be useful to have program read "Number Per Dimension" as the first string to automatically assign 10 to Var1 if thats possible. txt文件将具有所有顺序相同的值,尽管如果可能的话,让程序读取“每维数字”作为第一个字符串自动为Var1分配10可能很有用。

I'm guessing the first step is somehow to split it into two strings with ': ' as the delimiter and store it in some temporary string array variable? 我猜测第一步是以某种方式将其分为两个字符串,以':'作为分隔符,并将其存储在某些临时字符串数组变量中? I'm not sure what exactly is going on with the reading the line and storing the value in my code above. 我不确定读取该行并将值存储在上面的代码中到底发生了什么。

I would like to understand how the file stream actually works. 我想了解文件流实际上是如何工作的。 Sorry that the formatting is bad. 抱歉,格式化错误。

I'm not going to write you some code since you have one above, so I'm going to explain to you how I would do it/and how those work and give you some useful links. 因为上面有一个代码,所以我不会为您编写一些代码,因此,我将向您解释我将如何做/这些代码如何工作并为您提供一些有用的链接。

Use std::getline(file_name,line) to read a line. 使用std::getline(file_name,line)读取一行。 getline() has a default delimiter the \\n . getline()具有默认的定界符\\n So it stores the entire line in the variable line . 因此它将整个行存储在变量line

Having stored in a string variable what getline() returned, use std::find to find the ":" and it's position.It will return a size_t position ,use it with substr and/or erase functions to get these two parts in different variables. getline()返回的内容存储在字符串变量中后,使用std::find查找":"及其位置。它将返回size_t position ,将其与substr和/或erase函数结合使用,以将这两个部分分开变量。

String variable string var_text with your text will remain a it is, but the value since you want it as int or double needs modifications. 字符串变量string var_text与您的文本将保持原样,但由于要将其作为intdouble需要修改。 You will have to compare first the string var_text with any string you want. 您必须首先将string var_text与所需的任何字符串进行比较。 Depending on the result, you can set the variable to either int or double etc. with stringstream 根据结果​​,您可以使用stringstream将变量设置为intdouble等。

Now that you have your variable in the form you need, you can store it in the proper class member. 现在,您可以使用所需形式的变量,可以将其存储在适当的类成员中。

Repeat this for as long as getline() returns 1. 只要getline()返回1,重复此操作。

Try something more like this instead: 尝试类似这样的方法:

#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;
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM