Using loops to put data from file into struct

Hi,
I have to put some data from a txt file into a struct but I have no idea how to do this. Here is some information:

I have a structure that looks like this:
1
2
3
4
5
6
7
8
9
structure system
{
string datum;
int energie;
int max;
int min;

}nr1;


My txt file looks like this:
1
2
3
4
5
6
7
8
9
26.08.2011;00000123;4.738;00000165;
27.08.2011;00000137;5.564;00000185;
28.08.2011;00000140;3.123;00000192;
29.08.2011;00000146;4.823;00000201;
30.08.2011;00000151;5.003;00000211;
31.08.2011;00000160;5.677;00000225;
01.09.2011;00000166;3.120;00000235;
02.09.2011;00000171;4.123;00000246;

where the delimiter is the semicolon


A part from my programm:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
nr1= new struct system[integer_nr];

for(int i=0;!file.eof();i++)
{

    getline(file,data,';');

    if(i<((*start_line*4)-4)||i>(*end_line*4)-1) continue;
    //Now because I reached the line that I look for in the file i want to get         the string parts and put them in the structure,but how can I do this??

// I have tried like this, with no success:

nr1[i].datum=data;
nr1[i].energie=data;
nr1[i].max=data;
nr1[i].min=data;

// All this variables get the same data
//I want to loop through all the variables and put in them the part strings I get from the file, like this:
first part string is: 26.08.2011 that should be saved in the variable nr1[i].datum than next part string  00000123 that should be saved in the next variable of the structure nr1[i].energie=data and so on



j++;




}

delete[]anlage;
  file.close();


Can you help me with an advice or piece of code?
Note that I have to use dynamic arrays!

Thank you for your time
Suppose that the delimitir is ' ' (space) ¿can you parse it?
No the delimiter is the semicolon. When I use the getline function i can parse the string to part strings. But my problem is that I can not put this part strings into the variables of the structure.
I hope you have a solution for me.

Thank you
So you already isolated the parts that you need. And you only need a way to convert the string into a number.
¿Is that right? http://www.cplusplus.com/articles/Sy86b7Xj/
Yes this is true. I have to convert the string into numbers and than to put them into the struct.
Can you tell me what king of loop I should use to put this data into the struct?
Sorry about my english, probably this is why you can not understand what I have to do.
I know that this should be very simple but I have no idea how to do it.
How can I put this part strings into the struct using loops or something else?
Please help!

Thank you
There are a few things I noticed:
1
2
3
4
5
6
7
8
9
10
structure system // should be struct and 'system' is not a great name because it clashes with a standard function
{
string datum;

// one of these is not an int in the file..? I guess energie
int energie; // should be double?
int max; 
int min;

}nr1; // no reason to define an instance here 

So this is probably better:
1
2
3
4
5
6
7
8
9
struct system_t // change name
{
	std::string datum;

	// rearranged to match the order in the file (I assume)
	int min;
	double energie; // now a double
	int max;
};


For reading the datum I would use std::getline(file, sys.datum, ';');, however the other values, being numeric can be read using operator >> like file >> sys.energie;. You can skip the semicolons using: file.ignore();
Last edited on
yes you are right! there should be struct and not structure.
And double is much better! I only have to manage to put the data into the struct!
If i have a problem I will write here.
Thank you so much for helping me!
Can you tell me how can iterate through the variables in the struct and save there my data?
I have a dynamic array struct and I need to use loops.
Thank you
Do you have to use a dynamic array for the assignment? Because a std::vector would be much easier and safer.
yes I have to use dynamic arrays
Ok, i have managed to do this like you said.
I used the "convert" method to get numbers out of strings.

and than I used for loops to put the data into the struct.

Thank you for help!
Topic archived. No new replies allowed.