reading data from a file and storing it in variables


#ifndef SEARCH_H
#define SEARCH_H

class Search
{
private:
int ID;
int Health;
double Eccent;
public:
void GetID(int id) {ID = id;}
void GetHealth(int H) {Health = H;}
void GetEccent(double Ecc) {Eccent = Ecc;}
};
#endif





#include <iostream>
#include <ftream>
#include <string>
#include "search.h"

using namespace std;

void main(void)
{
Search Student;
string line;
ifstream data;
data.open("C:\\current.txt", ios::in);
if(data.is_open)
{
while(!data.eof())
{
getline(data, line, ':')
{
data>> line;
cout<<line<<end;
}
}
}
}

//this is the file im trying to read.
ID: 01
Health: 000
Eccent: 0.6012916565E-003

ID: 02
Health: 000
Eccentricity: 0.1111078262E-001

ID: 03
Health: 000
Eccentricity: 0.1513051987E-001
What exactly is the problem?

I think you might be trying to ask how to parse each line. If so, then you will have to write logic that will go through each line and look for specific characters. For example, the character ":" can be used as a separator where content prior is a key and content after is a value.

This might be useful to you: http://www.cplusplus.com/reference/clibrary/cstring/strtok/

I'm not exactly sure what your problem is so I don't want to write more than what is needed.
thepedestrian thanks for the reply.

I have those values stored in a txt file. my goal is to load those values back into my class variables. so the first time the ID value would be stored 01 then when it second time it would be a value of 02 and so forth it would load a list of values into the variable for uploading. I was able to read for a file but i dont have any logic in for it to store the data. Thats were i got stucked
Topic archived. No new replies allowed.