Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR CPP

read struct from file c++

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

struct patientRec {
	int id;
	string firstName;
	string lastName;
	float height;
	float weight;
};
int main() {
	patientRec patient;
	fstream file;
	file.open("Patient record.txt", ios::in);
	cout << "Patient record file: 
";
	if (file.is_open()) {
		file >> patient.id >> patient.firstName >> patient.lastName >> patient.height >> patient.weight;
		cout << "ID: " << patient.id << endl;
		cout << "First name: " << patient.firstName << endl;
		cout << "Last name: " << patient.lastName << endl;
		cout << "Height: " << patient.height << endl;
		cout << "Weight: " << patient.weight << endl;
      	file.close();

	}
}
 
PREVIOUS NEXT
Tagged: #read #struct #file
ADD COMMENT
Topic
Name
4+8 =