Fstream with dynamic arrays

Hey again, just another quickie;

I'm writing a program to create and store profiles for a selected number of people. I've created a dynamic array to store the profiles (each of which is a struct containing 6 pieces of data).

My question is this: how would I go about storing the profiles in a text file for a kind of "save/load" operation? I've read about fstream operations but I'm not sure how to implement them in the case of dynamic arrays.

I'll paste my code below!

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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

struct Profile
{
	string name;
	string hospitalOfBirth;
	string city;
	int dayOfBirth;
	int monthOfBirth;
	int yearOfBirth;
};

int main()
{
	int profileNum = 0, i;

	cout << "Enter the number of profiles: ";
	(cin >> profileNum).ignore();
	

	Profile* personProfile = new (nothrow) Profile[profileNum];

	if (personProfile == 0)
		cout << "Error: memory could not be allocated.";

	else
	{

		for(i = 0; i < profileNum; i++)
		{
			cout << "Enter the name of this person: ";
			getline(cin, personProfile[i].name);

			cout << "Enter the hospital of this person's birth: ";
			getline(cin, personProfile[i].hospitalOfBirth);
			
			cout << "Enter the city of residence of this person: ";
			getline(cin, personProfile[i].city);

			cout << "Enter the day of birth of this person (DD): ";
			cin >> personProfile[i].dayOfBirth;

			cout << "Enter the month of birth of this person (MM): ";
			cin >> personProfile[i].monthOfBirth;

			cout << "Enter the year of birth of this person (YYYY): ";
			cin >> personProfile[i].yearOfBirth;
			cin.ignore();

			cout << endl;
		}

		for(i = 0; i < profileNum; i++)
		{
			cout << "Name: " << personProfile[i].name << endl;

			cout << "Hospital: " << personProfile[i].hospitalOfBirth << endl;
			
			cout << "City: " << personProfile[i].city << endl;

			cout << "Date of Birth: " << personProfile[i].dayOfBirth << "/" << personProfile[i].monthOfBirth << "/"
				<< personProfile[i].yearOfBirth << endl;

			cin.ignore();
		}
	}
	
	delete [] personProfile;

	cin.ignore();
	return 0;
}


Any help is appreciated!

Thanks alot,
Ed
Nobody?? :(
you could start by giving your Profile the ability to be extracted or inserted onto a stream, and, while you're at it, return to using vectors:

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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include <iostream>
#include <vector>
#include <limits>
#include <fstream>
#include <string>

struct Profile
{
    std::string name;
    std::string hospitalOfBirth;
    std::string city;
    int dayOfBirth;
    int monthOfBirth;
    int yearOfBirth;
};
std::istream& operator>>(std::istream& is, Profile& p)
{
    Profile new_p;
    if(    getline(is, new_p.name)
        && getline(is, new_p.hospitalOfBirth)
        && getline(is, new_p.city)
        && is >> new_p.dayOfBirth >> new_p.monthOfBirth >> new_p.yearOfBirth)
    {   
        is.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
        p = new_p;
    }
    return is;
}

std::ostream& operator<<(std::ostream& os, const Profile& p)
{
    return os << p.name << '\n'
              << p.hospitalOfBirth << '\n'
              << p.city << '\n'
              << p.dayOfBirth << ' ' << p.monthOfBirth << ' ' << p.yearOfBirth;
}

int main()
{   

    std::ifstream f("records.txt");

    std::vector<Profile> personProfile;
    Profile p;
    while(f >> p)
       personProfile.push_back(p);

    std::cout << "File contained " << personProfile.size() << " records:\n";
    for(const auto& p: personProfile)
        std::cout << p << '\n';
}
Umm.. thanks..
Topic archived. No new replies allowed.