How to read data from text file.

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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#include <iostream>
#include <fstream>
#include <cstring>
using namespace std;

const int MAX = 80;
const int MAXNO = 5;

enum Title {Miss, Mrs, Mr, Dr, Unknown};

struct Date
{
	int day;
	int month;
	int year;
};

struct MyInfo
{
	char name [MAX];
	Title title;
	char nationality [MAX];
	Date dob;	 
	int noOfHobbies;
	char hobby [MAXNO] [MAX];
	int noOfWishes;
	char wish [MAXNO] [MAX];
};

void getMyInfo (fstream&, char[], MyInfo&);
string convertmonth(MyInfo&);
void displayMyInfo(MyInfo&);

int main()
{
		srand(time (NULL));
		fstream afile;
		char filename [MAX];
		
		cout << "Enter your info file name: ";
		cin >> filename;
		MyInfo a;
		getMyInfo(afile,filename,a);
		displayMyInfo(a);
		
}

void getMyInfo(fstream& afile, char filename [],MyInfo& a)
{
	afile.open(filename, ios::in);
	if (!afile)
	{
		cout << "file opened for reading failed" << endl;
		exit (-1);
	}
	cout << endl;
	cout << "Begin reading of " << filename << endl;
	cout << "File name " << filename << " closed for reading" << endl;
	cout << endl; 
	afile.getline(a.name, MAX);
	afile.getline(a.nationality, MAX);
	afile >> a.dob.day;
	afile.ignore(3);
	afile >> a.dob.month;
	afile.ignore(3);
	afile >> a.dob.year;
	afile >> a.noOfHobbies;
	afile.clear ();
	afile.ignore (100, '\n');
	int MAXNO=1;
	while(MAXNO<=a.noOfHobbies)
	{  
	afile.getline(a.hobby[MAXNO], MAX);
	MAXNO++;
	}
	afile >> a.noOfWishes;
	afile.clear ();
	afile.ignore (100, '\n');
	MAXNO=1;
	while(MAXNO<=a.noOfWishes)
	{  
	afile.getline(a.wish[MAXNO], MAX);
	MAXNO++;
	}
	afile.close ();
}

string convertmonth(MyInfo& a){
  switch (a.dob.month)
  {
    case 1: return "January";
    case 2: return "February";
    case 3: return "March";
    case 4: return "April";
    case 5: return "May";
    case 6: return "June";
    case 7: return "July";  
    case 8: return "August";
    case 9: return "September";
    case 10: return "October";
    case 11: return "November";
    case 12: return "December";

  }                  

}
 
void displayMyInfo(MyInfo& a)
{
	cout << "My Information" << endl;
	cout << "Name: " << a.name << endl;
	cout << "National: " << a.nationality << endl;
	cout << "Date of birth: " << a.dob.day << " " << convertmonth(a) << ", " << a.dob.year << endl;
	cout << "I have " << a.noOfHobbies << " hobbies" << endl;
	for(int MAXNO=1;MAXNO<=a.noOfHobbies;MAXNO++)
	{
	cout << "  " << MAXNO << ": " << a.hobby[MAXNO] << endl;
	}
	cout << "I have " << a.noOfWishes << " wishes" << endl;
	for(int MAXNO=1;MAXNO<=a.noOfWishes;MAXNO++)
	{
	cout << "  " << MAXNO << ": " << a.wish[MAXNO] << endl;
	}
}


The above is working fine except i'm not able to get the title.
Below is the text file that i'll get the data from.
if i remove the title(Dr) it's working fine but as long as i put in the Dr it'll be having problem.
Can someone guide me how can i grab both title and country from same line?


Ethan
Dr Country
03 - 12 - 2012
2 // No of hobbies
Soccer
Gaming
4 // No of wishes
No1 wish
No2 wish
No3 wish
No4 wish

Last edited on
Please use code tags when posting
http://www.cplusplus.com/articles/z13hAqkS/

I ran your code it seems to run fine. I did have to add #include <string> and <ctime>


The output I got
Enter your info file name: input.txt

Begin reading of input.txt
File name input.txt closed for reading

My Information
Name: Ethan
National: Dr Country
Date of birth: 3 December, 2012
I have 2 hobbies
 1: Soccer
 2: Gaming
I have 4 wishes
 1: No1 wish
 2: No2 wish
 3: No3 wish
 4: No4 wish
Last edited on
Hi Yanson yes it's working fine.
but you can see inside myinfo struct I've include title.
So Dr in front of Country is for title.
I've declared titles inside enum.
Now I need to grab the title from text file and display it at the output.
But i'm not able to grab title and country from same line so i'm trying to figure out about it.
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#include <iostream>
#include <fstream>
#include <cstring>
#include <ctime>
#include <string>
using namespace std;

const int MAX = 80;
const int MAXNO = 5;

struct Date
{
	int day;
	int month;
	int year;
};

struct MyInfo
{
	char name[MAX];
	char title[MAXNO];
	char nationality[MAX];
	Date dob;
	int noOfHobbies;
	char hobby[MAXNO][MAX];
	int noOfWishes;
	char wish[MAXNO][MAX];
};

void getMyInfo(fstream&, char[], MyInfo&);
string convertmonth(MyInfo&);
void displayMyInfo(MyInfo&);

int main()
{
	srand(time(NULL));
	fstream afile;
	char filename[MAX];

	cout << "Enter your info file name: ";
	cin >> filename;
	MyInfo a;
	getMyInfo(afile, filename, a);
	displayMyInfo(a);
	cin.ignore();
	cin.ignore();
	return 0;
}

void getMyInfo(fstream& afile, char filename[], MyInfo& a)
{
	afile.open(filename, ios::in);
	if (!afile)
	{
		cout << "file opened for reading failed" << endl;
		exit(-1);
	}
	cout << endl;
	cout << "Begin reading of " << filename << endl;
	cout << "File name " << filename << " closed for reading" << endl;
	cout << endl;
	afile.getline(a.name, MAX);
	afile.getline(a.title, MAXNO, ' ');
	afile.getline(a.nationality, MAX);
	afile >> a.dob.day;
	afile.ignore(3);
	afile >> a.dob.month;
	afile.ignore(3);
	afile >> a.dob.year;
	afile >> a.noOfHobbies;
	afile.clear();
	afile.ignore(100, '\n');
	int MAXNO = 1;
	while (MAXNO <= a.noOfHobbies)
	{
		afile.getline(a.hobby[MAXNO], MAX);
		MAXNO++;
	}
	afile >> a.noOfWishes;
	afile.clear();
	afile.ignore(100, '\n');
	MAXNO = 1;
	while (MAXNO <= a.noOfWishes)
	{
		afile.getline(a.wish[MAXNO], MAX);
		MAXNO++;
	}
	afile.close();
}

string convertmonth(MyInfo& a){
	switch (a.dob.month)
	{
	case 1: return "January";
	case 2: return "February";
	case 3: return "March";
	case 4: return "April";
	case 5: return "May";
	case 6: return "June";
	case 7: return "July";
	case 8: return "August";
	case 9: return "September";
	case 10: return "October";
	case 11: return "November";
	case 12: return "December";

	}

}

void displayMyInfo(MyInfo& a)
{
	cout << "My Information" << endl;
	cout << "Name: " << a.name << endl;
	cout << "Title: " << a.title << endl;
	cout << "National: " << a.nationality << endl;
	cout << "Date of birth: " << a.dob.day << " " << convertmonth(a) << ", " << a.dob.year << endl;
	cout << "I have " << a.noOfHobbies << " hobbies" << endl;
	for (int MAXNO = 1; MAXNO <= a.noOfHobbies; MAXNO++)
	{
		cout << " " << MAXNO << ": " << a.hobby[MAXNO] << endl;
	}
	cout << "I have " << a.noOfWishes << " wishes" << endl;
	for (int MAXNO = 1; MAXNO <= a.noOfWishes; MAXNO++)
	{
		cout << " " << MAXNO << ": " << a.wish[MAXNO] << endl;
	}
}



Enter your info file name: input.txt

Begin reading of input.txt
File name input.txt closed for reading

My Information
Name: Ethan
Title: Dr
National: Country
Date of birth: 3 December, 2012
I have 2 hobbies
 1: Soccer
 2: Gaming
I have 4 wishes
 1: No1 wish
 2: No2 wish
 3: No3 wish
 4: No4 wish
Topic archived. No new replies allowed.