0

I have a read function which takes numbers from a text file and stored them into a data structure. I have made this function.

void VectorIntStorage::read(ifstream &in)
{
    if(in.is_open())
    {
        for (int i = 0; in && i < n; ++ i) 
        {
            in >> vectorStorage<i>;
        }
    }
}

I am trying to add them into a vector structure, is this code correct??

2 Answers 2

2

No, it isn't. The canonical way would be:

vector <int> v;
int n;

while( f >> n ) {
    v.push_back( n );
}

where f is an ifstream.

Sign up to request clarification or add additional context in comments.

Comments

0

No, if you write you code this way the compilation will fail. Maybe you can allocate enough space for the vector and then store the date the ifstream read.

vector<int> v(MAX_SIZE);
int iIndex = 0;

while((iIndex < v.size()) && (in >> v[iIndex]))
{
    ++iIndex;
}

1 Comment

This is so bad... Looks like C, that has been hit by a truck. Seriously. What was happening in the person's head while they were writing this?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.