0

Im beginner in C++ I have code:

    std::ifstream inFile(makeFolder/"file");
    std::string str[3];
    while(!inFile.eof())
    {
        for(int i = 0; i < 1; ++i)
            inFile >> str[i];
    }

        
        for(int i = 0; i < 3; ++i)
            std::cout << str[i];

and text file with:

123\n
456\n
789\n  

When I execute this program I have out to console:

123
456
789

But if I change i to 2 in cycle where I init str I have this out:

789
456

And if I change i to 1 in cycle where I init str I have this out:

789


Why I have this out? I must be see out if I input i=1 123 and if i=2456.

Ivan Raih
  • 21
  • 1
  • 2
    You may want to read [Why is iostream::eof inside a loop condition (i.e. `while (!stream.eof())`) considered wrong?](https://stackoverflow.com/q/5605125/11082165) – Brian61354270 Dec 10 '21 at 16:39
  • 3
    Your code snippet is confusing, why do you need a for loop inside the while loop? Just make a vector of strings: `while (inFile >> tempString) vec_of_strings.push_back(tempString);` – Mansoor Dec 10 '21 at 16:39
  • also you only initialize the first value of your `str` (which is an array of `std::string`, and then you're reading the whole array,... something is going wrong with your outputs – tony_merguez Dec 10 '21 at 16:42

0 Answers0