0

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]
  • You seem to have the reading part down, so what do you mean by " assign the corresponding lines to each variable"? How are ft_means and ft_std_dev defined? – Botje Aug 26 '20 at 08:46
  • 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. ft_means and ft_std_dev are doubles [3] – Maklaizer Aug 26 '20 at 08:49
  • Any particular preference for the file format? Is it acceptable to have two numbers per line (mean and stddev), for example? Or do you want all the means first and then all the stddevs? – Botje Aug 26 '20 at 08:51
  • [Read this now](https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-i-e-while-stream-eof-cons) – n. 'pronouns' m. Aug 26 '20 at 08:51
  • Either, you group the values pair wise in file (so that you can read them consecutively) or you have to read all values in a container (e.g. a `std::vector`) which you might split in two halves (somehow) afterwards. – Scheff's Cat Aug 26 '20 at 08:52
  • I want to read all the means first and then all the stddevs – Maklaizer Aug 26 '20 at 08:52
  • You already know how to do that; just chop the resulting vector in half afterward. – Botje Aug 26 '20 at 08:54
  • @Botje so if have a vector of 866 elements how can I chop it into 5 doubles of different lengths, can you write me an example? – Maklaizer Aug 26 '20 at 08:59
  • 866 is not a multiple of 5? What do you mean by "5 doubles of different lenghts"? Do you know in advance what those lengths are or do you have to deduce it from the read numbers? It would help if you specified what your file format was. – Botje Aug 26 '20 at 09:00
  • @Botje Is it possible to create a for loop for example and control the lines assigned to each variable? Because I have different types of lines in the text file and I don't read them all the same way. – Maklaizer Aug 26 '20 at 09:09

2 Answers2

1

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. The std::istringstream object turns a string into a stream so you can use the regular extraction operator on it.

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.

Botje
  • 15,729
  • 2
  • 22
  • 32
0
  1. Write a function to get the x number of values stored in each file
  2. Store it in some variable x
  3. Now iterate the file content again by using value x. so that you'll know how much to iterate and allocate before hand
Samir Kape
  • 807
  • 7
  • 15
  • 1
    By necessity `var[x]` will be a variable length array (VLA). [C++ does not have VLAs](https://stackoverflow.com/questions/39334435/variable-length-array-vla-in-c-compilers) – Botje Aug 26 '20 at 09:07