Write/Read structure to file

I have tried to write a struct to a file - with success. Now i'm trying to be able to read the same file back into my programme: however I keep getting an error

No operator matches these operands

My struct is
1
2
3
4
5
6
7
8
9
10
11
12
13
struct member_details
{
	float	member_length,																				//to store member length
			member_width,																				//to store member width
			member_depth,																				//to store member depth
			member_qk_factored_load,																	//to store unfactored load
			member_gk_factored_load,																	//to store factored load
			member_max_moment,																			//to store moment
			member_shear;																				//to store shear value
	int		member_type,																				//to store member type for quicker search
			member_no;																					//to display record number
	std::string	member_name;																			//to store member name e.g. beam or column
}record[100];


and the code is

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
34
35
//load member details from file
void member_dimensions_file()
{
	 int	i,																						//declare and set int for loop
			no_of_members=100;																		//declare variable for while loop so that user enters a value less than 15
	string	filePath;																				//declare string for file name
	ifstream  dimensionsInFile;																		//declare input filestream name
	//cout<<"Enter full path name of file to save to"<<endl;										//Ask user to define file name to save to 
	//cin>> filePath;																				//user input for file path name
	dimensionsInFile.open ("test.dat");												
	if (!dimensionsInFile)
		{
			cout<<"Cannot load file"<<endl;
			return ;
		}
	else
		{
			for (i=0; i < no_of_members; i++)																//start of loop
				{	// write struct data from file	
					dimensionsInFile>>
					&record[i].member_depth,		
					&record[i].member_length,
					&record[i].member_width,
					&record[i].member_qk_factored_load,															
					&record[i].member_gk_factored_load,															
					&record[i].member_max_moment,																			
					&record[i].member_shear,																			
					&record[i].member_type,																				
					&record[i].member_no;	
					cout<<" Member no "<<i<<"stored"<<endl;
				}
			cout <<"All members have been successfully loaded"<<endl;
			dimensionsInFile.close();
		}
}


not sure if it will help, but the code used to write the file is:

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
34
35
void member_details_save()
{
	 int	i,																						//declare and set int for loop
			no_of_members=100;																		//declare variable for while loop so that user enters a value less than 15
	string	filePath;																				//declare string for file name
	ofstream  dimensionsOutfile;																	//declare output filestream name
	//cout<<"Enter full path name of file to save to"<<endl;											//Ask user to define file name to save to 
	//cin>> filePath;																					//user input for file path name
	dimensionsOutfile.open ("test.dat");												
	if (!dimensionsOutfile)
		{
			cout<<"Cannot load file"<<endl;
			return ;
		}
	else
		{
			for (i=0; i < no_of_members; i++)																//start of loop
				{	// write struct data from file	
					dimensionsOutfile<<
					&record[i].member_depth,		
					&record[i].member_length,
					&record[i].member_width,
					&record[i].member_qk_factored_load,															
					&record[i].member_gk_factored_load,															
					&record[i].member_max_moment,																			
					&record[i].member_shear,																			
					&record[i].member_type,																				
					&record[i].member_no;	
					cout<<" Member no "<<i<<"stored"<<endl;
				}
			cout <<"All members have been successfully saved"<<endl;
			dimensionsOutfile.close();
		}
	
}
Last edited on
The first thing I don't like is &record[i].member_depth.

I don't think you need to reference that, it's enough with record[i].member_depth.

The second think I see wrong is the way to print in the file.
You can concatenate strings using ',' so:

1
2
3
4
5
dimensionsOutfile<<
&record[i].member_depth,		
&record[i].member_length,
&record[i].member_width,
&record[i].member_qk_factored_load,	


It's wrong.
The way is:
1
2
3
dimensionsOutfile<<
record[i].member_depth <<	
record[i].member_length << ...


And if you want print the comma:
1
2
3
dimensionsOutfile<< ", " <<
record[i].member_depth << ", " <<
record[i].member_length << ...

thank you, I have now changed the code to your suggestion and it WORKS.... YEY.
Last edited on
Topic archived. No new replies allowed.