简体   繁体   English

从 C++ 中的文本文件中读取多个变量

[英]Read multiple variables from a text file in C++

I am assigning each variable for the moment using seperate text files.我暂时使用单独的文本文件分配每个变量。 I have something in this form:我有这种形式的东西:

double ft_means[3];
double ft_std_dev[3];
inputFile.open("mean.txt");
if (!inputFile)
cout<< "Error";
count=0;
while(!inputFile.eof())
{
    inputFile >> ft_means[count];
    count++;
}

count= 0;
ifstream inputFile2;
inputFile2.open("std_dev.txt");
if (!inputFile2)
cout<< "Error";
count=0;
while(!inputFile2.eof())
{
    inputFile2 >> ft_std_dev[count];
    count++;
}
char line[20];
std::string words[405];
int i=0;
std::ifstream myfile ("features.txt");
if (myfile.is_open())
    {
        for(i=0; i<3; ++i) {
        myfile.getline(line, 3);
        words[i]=line;
        strcpy(line, "");
    }
    myfile.close();
}

My current text files are in the following form:我当前的文本文件采用以下形式:

Mean.txt: 
11
23
36
std_dev.txt:
34
42
22
features.txt
abc[0]
abc[1]
abc[3]
abc[4]*abc[5]

My question is how can I assign the corresponding lines to each variable from the same text file.我的问题是如何将相应的行分配给同一个文本文件中的每个变量。 I want to no longer have seperate text files but only one text file that contains all the lines and then assign each variable to the corresponding lines of the text file.我不想再有单独的文本文件,而是只有一个包含所有行的文本文件,然后将每个变量分配给文本文件的相应行。 The new text file format can be for example in this form:例如,新的文本文件格式可以采用以下形式:

11
23
36

34
42
22

abc[0]
abc[1]
abc[3]
abc[4]*abc[5]

Assuming you have different blocks in your file, you can use the following structure:假设您的文件中有不同的块,您可以使用以下结构:

std::string line;

// output variables
std::vector<double> means;
std::vector<double> stddevs;
std::vector<std::string> expressions;

// part one: means
while (std::getline(inputFile, line) && !line.empty()) {
    double mean;
    std::istringstream(line) >> mean;
    means.push_back(mean);
}

// part two: stddevs
while (std::getline(inputFile, line) && !line.empty()) {
    double stddev;
    std::istringstream(line) >> stddev;
    stddevs.push_back(stddev);
}

// part three: expressions
while (std::getline(inputFile, line) && !line.empty()) {
    expressions.push_back(line);
}

The condition in each while loop checks that you have not reached the end of the file and that you have not just read an empty line.每个while循环中的条件都会检查您是否还没有到达文件的末尾,并且您没有读取一个空行。 The std::istringstream object turns a string into a stream so you can use the regular extraction operator on it. std::istringstream对象将字符串转换为流,因此您可以对其使用常规提取运算符。

You can add some using statements to bring the various classes in scope so you can get rid of some of the std:: noise, but that is up to you.您可以添加一些using语句来将各种类引入范围,这样您就可以摆脱一些std::噪音,但这取决于您。

  1. Write a function to get the x number of values stored in each file编写一个函数来获取每个文件中存储的 x 个值
  2. Store it in some variable x将其存储在某个变量 x 中
  3. Now iterate the file content again by using value x.现在使用值 x 再次迭代文件内容。 so that you'll know how much to iterate and allocate before hand这样您就可以事先知道要迭代和分配多少

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

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