Thread: Read data from file and put into vector

  1. #1
    Registered User
    Join Date
    Apr 2012
    Posts
    2

    Read data from file and put into vector

    Hi everyone. I'm new to this board and am just starting to learn C++. I'm having difficulties with part of a problem I'm trying to solve. I need to read data from a file, which contains integers representing date and time, and insert the data into a vector.

    My data file for appointments are layed out like so, representing month day year hour minute:

    05 16 2012 09 00
    08 30 2011 10 30

    My elementary vector so far looks like this:

    vector<Appointment*> fileData;
    fileData[0] = new Appointment(read_month_data_from_file, read_day_data_from_file, read_year_data_from_file, read_hour_data_from_file, read_minute_data_from_file);

    I've added the read_ parts to represent where the data needs to be transfered into.

    Any help would be greatly appreciated. Thanks for the time and help in advance.

  2. #2
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Overload the >> Operator for Appointment and get the values like you do with std::cin, only replacing it with the name of your object.

  3. #3
    Registered User
    Join Date
    Apr 2012
    Posts
    2
    Thanks for the reply manasij. I'm not seeing how that helps solve my problem (I'm new, so I'm most likely not making the connection properly).

    So I need to get data from:
    ifstream input_data;
    input_data.open("appointments.txt");

    And put that data into:
    vector<Appointment*> fileData;
    fileData[0] = new Appointment(read_month_data_from_file, read_day_data_from_file, read_year_data_from_file, read_hour_data_from_file, read_minute_data_from_file);

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Do you really need to store Appointment pointers? Why not use a std::vector<Appointment> instead? If you do have a good reason to use pointers, why not use a std::vector<std::shared_ptr<Appointment>> or std::vector<std::unique_ptr<Appointment>> instead?

    Quote Originally Posted by optimus203
    My elementary vector so far looks like this:

    vector<Appointment*> fileData;
    fileData[0] = new Appointment(read_month_data_from_file, read_day_data_from_file, read_year_data_from_file, read_hour_data_from_file, read_minute_data_from_file);
    One problem is that you need to actually create the element in the vector, e.g., by using push_back.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Mar 2011
    Posts
    12
    Code:
    int m, d, y, h, n;
    while (input_data >> m >> d >> y >> h >> n)
    	fileData.push_back(new Appointment(m, d, y, h, n));

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Loading data into a vector from text-file
    By Niels_M in forum C++ Programming
    Replies: 3
    Last Post: 04-01-2012, 06:31 AM
  2. Read numbers from file to std::vector
    By Petike in forum C++ Programming
    Replies: 2
    Last Post: 12-24-2010, 04:33 AM
  3. Opening file and loading data into Vector
    By soopah256 in forum C++ Programming
    Replies: 4
    Last Post: 08-04-2009, 10:18 PM
  4. How to read a file stream entirely into a vector?
    By jiapei100 in forum C++ Programming
    Replies: 4
    Last Post: 01-06-2008, 03:22 PM
  5. Replies: 21
    Last Post: 11-03-2007, 02:56 PM