Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8510571
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T03:44:31+00:00 2026-06-11T03:44:31+00:00

I have read in data from a file and want to store the data

  • 0

I have read in data from a file and want to store the data as a vector of objects.

vector <Thing*> thingVector;
for (int i = 0; i < 10; i++) {
// Read in contents of file
getline(fileName, v1, ',');
cout << v1 << endl;
getline(fileName, v1, ',');
cout << v2 << endl;
getline(fileName, v3, ',');
cout << v3 << endl;
getline(fileName, v4, '\n');
cout << v4 << endl << endl;
// Store
Thing* thingDetails = new Thing(v1, v2, v3, v4);
thingVector.push_back(thingDetails);
delete thingDetails;
}
thingFile.close();
cout << "Size of THING vector is " << thingVector.size() << endl; // Displays 10

cout << thingVector[0].getV1 << endl; // ERROR HERE

How can I store each record in the vector and then access the data?

I also tried to do it like this:
thingVector.push_back(Thing(v1, v2, v3, v4));

I didn’t have the last and third to last lines in the for statement when I tried it like this, but I couldn’t access the data so gave up on this method.

Any suggestions?

THING .H FILE

#ifndef THING_H
#define THING_H

#include <string>

using namespace std;

class Thing {
public:

Thing(string v1, string v2, string v3, string v4);
string getV1();
string getV2();
string getV3();
string getV4();

private:
string v1;
string v2;
string v3;
string v4;
};

#endif

THING .CPP FILE

#include "thing.h"

#include <string>

using namespace std;
Thing::Thing(string aV1, string aV2, string aV3, string aV4) {
v1 = aV1;
v2 = aV2;
v3 = aV3;
v4 = aV4;
}

string Thing::getV1(){
return v1;
}

string Thing::getV3(){
return v2;
}

string Thing::getV3){
return v3;
}

string Thing::getV4(){
return v4;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T03:44:33+00:00Added an answer on June 11, 2026 at 3:44 am

    You should use

    vector <Thing> thingVector;
    ...
    thingVector.push_back(*thingDetails);
    

    This will pass object rather than the pointer.

    Also

    cout << thingVector[0].getV1() << endl; // Convert to a function call.
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have Uitable with data read from a AScii file. I want to select
I'm trying to display image data read in from a binary file (I have
I have an xml file(from federal government's data.gov) which I'm trying to read with
Okay, so I want to have a generic method that can read data from
I have a program that load data from a file using std::ifstream and store
I want to read N bytes of data from a file stream and append
I have a C++ program that will read in data from a binary file
I'm trying to read data in from a binary file and then store in
I have made a code that read data from flash Nand (without filesystem). fd
I have a static initialization block where I read data from DB into certain

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.