Elias Gomez
Elias Gomez

Reputation: 65

Displaying contents of vector

I am having trouble displaying the contents of a vector. I am unsure whether it is the way I read in the values from the text file, or whether my display function is just not working.

    #include <iostream>
#include <string>
#include <vector>
#include <fstream>

using namespace std;

int main()
{
    string line;
    int n,length;
    std::vector<int>arr1;
    fstream file("t1.txt");
    if(file.is_open())
    {
        while (getline(file,line))
        {
            cout << line << endl;
        }
        file << line;
        length = line.length();
        while(file >> n)
            arr1.push_back(n);
        for(int i =0; i < (int)arr1.size(); i++)
            cout << arr1.at(i) << endl;
    }
    return 0;
}

Any help would be greatly appreciated.

the text file contains "5 2 5 5 -1 7 2 5 3 5 2 -2"

Upvotes: 1

Views: 102

Answers (1)

VolAnd
VolAnd

Reputation: 6407

I suppose you have a problem with vector building (reading values from file to vector). You should delete (or comment) expression with getline, as well as writing line back to file (file << line), and use only file >> n.

Try the following shortened version of your program

#include <iostream>
#include <string>
#include <vector>
#include <fstream>

using namespace std;

int main()
{
    string line;
    int n;
    std::vector<int> arr1;
    fstream file("t1.txt");
    if(file.is_open())
    {
        while(file >> n)
            arr1.push_back(n);
        for(int i =0; i < (int)arr1.size(); i++)
            cout << arr1.at(i) << endl;
        file.close();
    }
    return 0;
}

Problem of your initial code is that after you read line from your file (that comprises only one line) next reading (whether it is file >> ... or getline(...)) will provide no data because END OF FILE. To read data from file again you should re-open it

        ...
        length = line.length();
        file.close();
        file.open("t1.txt");
        while(file >> n)
            arr1.push_back(n);
        ...

or rewind to the beginning

        ...
        length = line.length();
        file.clear();
        file.seekg(0, ios_base::beg);
        while(file >> n)
            arr1.push_back(n);
        ...

But you should understand, that writing to file stream does not provide desired effect - the recorded data will be available only after transfer written data from the buffer to the file (in the general case, after the file closing).

EDIT:

Also I want to add, that C++ allows input to and output from vector without additional loops, just consider the following example

    if(file.is_open())
    {
        // filling vector from the stream
        istream_iterator<int> it(file); // iterator for int values in file
        istream_iterator<int> eos; // end of straem
        arr1.insert(arr1.begin(), it, eos);
        // output vector to the stream (cout = standard output stream)
        ostream_iterator<int> oit (cout,"\n"); // "\n" = new line is a separator for output
                                               // also " " or any other separator can be used
        copy(arr1.begin(), arr1.end(), oit);
    }

Upvotes: 3

Related Questions