I want to read data into an array from a data file. Because I do not know, ahead of time, how much data is contained within the file I need to declare the array at run time using new.

My current strategy is the read through the data file to find out how many data elements there are. Then I declare the array using the number of data elements I learned from the first read. Then I read through the data file a second time to place items into the array.

Is this the most efficient way to accomplish this task?

In general, it seems that reading and writing to the hard disk is the slowest thing that a program will do. So, if I could accomplish my task by reading through the file only once it would make the program faster.

The question is: how do I do that?

Suggestions?

Recommended Answers

All 4 Replies

If you have an unknow amount of data to read then you can read in each object/line from the file and store it into a vector. Lets say you have a file containing only integer data. You coud read that with:

std::vector<int> data;
std::ifstream fin("name_of_file.txt");

if(!fin)
    std::cout << "unable to open file!"
else
{
    int temp;
    while (fin >> temp)
        data.push_back(temp);
}
commented: This should be a code snippet =] +6

I guess I have to learn about vectors. Is there an online tutorial about them available?

Can there be a vector of structures?

I am guessing that with data.push_back(temp) the .push_back(temp) is a feature of the vector type. But, it does not seem to indicate a specific element identification in the vector data. How would you access something like the 123rd element in the vector?

What happens if you try to access the 123rd element and there are only 120 data items recorded? With an array it would return giberish (some random value that was stored in that memory location). What would a vector do?

To prevent that, we would still need to know how many elements were stored in the vector. I suppose a counter could be placed with the while loop. But, does the count start with zero (like an array)?

std::vector is a dynamically resizing array. This is one of the first standard containers you should learn how to use. It has a [] operator so you can use it like an array and get any element and it also has a size() function that will tell you how many elements are in it. You can read all about it here on cppreference

Can there be a vector of structures?

Yes

I am guessing that with data.push_back(temp) the .push_back(temp) is a feature of the vector type. But, it does not seem to indicate a specific element identification in the vector data. How would you access something like the 123rd element in the vector?

struct Student
{
    string name;
    string id;
    /*
    //ctor... but not used / needed in this demo //
    Student( const string& name="", const string& id="" ) 
        : name(name), id(id) {}
    */
    void takeIn()
    {
        cout << "Enter name: " << flush;
        getline( cin, name );
        cout << "Enter id: " << flush;
        getline( cin, id );
    }
    print() const
    {
        cout << name << ", " << id << '\n'; 
    }
} ;

// ...other stuff ...

bool more(); // need to define these before calling below //

void print( const vector< Student >& studs )
{
    for( size_t i = 0; i < studs.size(); ++ i )
    {
        //cout << studs[i].name << ", " << studs[i].id << '\n';
        studs[i].print();
    }
}


int main()
{
    // get an empty vector to hold 'Student objects'
    vector< Student > yourStuds;

    do
    {
        Student tmp;
        tmp.takeIn();
        yourStuds.push_back( tmp ); 
    }
    while( more() );

    cout << "Showing all the students:\n";
    print( yourStuds );
}

What happens if you try to access the 123rd element and there are only 120 data items recorded? With an array it would return giberish (some random value that was stored in that memory location). What would a vector do?

Look up the vector call: studs.at(index);

To prevent that, we would still need to know how many elements were stored in the vector. I suppose a counter could be placed with the while loop. But, does the count start with zero (like an array)?

See above.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.