0

I want to create a file which stores some digits, later to be extracted into an array.

#include <vector>   
#include <fstream>  
#include <iostream>   
#include <stringstream>

//for setw()
#include <iomanip>


std::vector<int> getData()
{
    using namespace std;

    //For the sake of this question simplicity
    //I won't validate the data
    //And the text file will contain these 10 digits:
    //1234567890
    ifstream in_file("altnum.txt");

    //The vector which holds the results extracted from in_file 
    vector<int> out;

    //It looks like C++ doesn't support extracting data 
    //from file to stringstream directly
    //So i have to use a string as a middleman
    stringstream ss;
    string str;

    //Extract digits from the file until there's no more
    while (!in_file.eof())
    {
        /*
            Here, every block of 4 digits is read
            and then stored as one independent element
        */
            int element;
            in_file >> setw(4) >> str;
            cout<<str<<"\n";

            ss << str;
            cout<<ss.str()<<"\n";

            ss >> element;
            cout<<element<<"\n";

            out.push_back(element);
    }

    //Tell me, program, what have you got for my array?
    for (auto &o : out)
        cout << o << ' ';

    in_file.close();
    return out;
}

When i run the snippet of code above, i get the following numbers:

1234 1234 1234

while

1234 5678 90

is expected.

And then i found out (by cout-ing each variable to the screen) that the stringstream ss does not release its content when extracted to 'element'

But why is that? I thought that like cin stream, after the extraction, the stream would pop data out of it? Did i miss anything extremely important?

Revol Noom
  • 61
  • 7
  • Why do you think `std::stringstream` should loose that content after data extraction? That would prevent functions like `seek(0)` from working. We've been closing your [previous question](https://stackoverflow.com/questions/56502902/stringstream-doesnt-lose-data-when-extracted) for reasons, so why do you ask again? – πάντα ῥεῖ Jun 08 '19 at 12:30
  • I'm sorry, but i haven't found an answer to the question after a long time rummaging on the internet. And my previous question was invalid (since i asked 3 things in one post). So i thought that maybe i should delete it and do a new one. – Revol Noom Jun 08 '19 at 12:35
  • OK. Anyways, just extracting values usually doesn't change the streams content. `std::cin` is a bit of an exception in this case, since `seek()` operations don't make sense in this particular case. Also read [this](https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-i-e-while-stream-eof-cons) please. – πάντα ῥεῖ Jun 08 '19 at 12:39
  • @Revol Noom Use ss.clear() after each ss >> element; – Vlad from Moscow Jun 08 '19 at 12:41
  • Since cin lose its content immediately after extraction, and i haven't learnt much about streams, i thought that stringstream would be somewhat like cin. Thank you for your answer! I understand it now. – Revol Noom Jun 08 '19 at 12:44
  • @Revol Noom Take into account that the condition in the loop is selected incorrectly while (!in_file.eof()). You have to check each operation in_file >> setw(4) >> str; – Vlad from Moscow Jun 08 '19 at 12:44
  • eof() is something i learned a year back when i still code with Pascal. So C++ people don't use it? I will look into that. Thanks Vlad. – Revol Noom Jun 08 '19 at 12:47
  • 1
    Concerning `istream::eof()`: [SO: Why is iostream::eof inside a loop condition (i.e. `while (!stream.eof())`) considered wrong?](https://stackoverflow.com/a/5605159/7478597) – Scheff's Cat Jun 08 '19 at 12:54

0 Answers0