Copying text file to struct array

Pages: 12
The textfile looks liket his:

Martin Smith/ 22 2.2
Austin Clinton/ 18 3.1
Johnson/ 19 2.9
Maggie Jones/ 23 2.3
Tyler W Brown/16 3.4

I have to copy the data into a struct array of:

1
2
3
4
5
6
7
8
9
10
const int N = 5;

struct RECORD
{
	char Name[15];
	int Age;
	float Gpa;
};

RECORD p[N]; float GpaAve;


When I try and copy the data and display all of the data, it only displays the first line of the text file data, how do I fix this?

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
#include <iostream>
#include <array>
#include <fstream>
#include <cstring>


using namespace std;

void CopyRecords(string Fname);
void Display();
void Toupper();

const int N = 5;

struct RECORD
{
	char Name[15];
	int Age;
	float Gpa;
};

RECORD p[N]; float GpaAve;

int main()
{


	// Copy data from file to structure array p 
	CopyRecords("data2.txt");
	
	// Display uppercase 
	Display();

	// Make Name variable all uppercase
	Toupper();




	system("PAUSE");
	return 0;
}

void CopyRecords(string Fname)
{


	fstream f;
	f.open(Fname, ios::in);

	for (int i = 0; i < N; i++)
	{

		f.getline(p[i].Name, 15, '/');
		f >> p[i].Age >> p[i].Gpa;
	}

	f.close();
}


void Display()
{
	
	
	for (int i = 0; i < N; i++)
	{
		cout << p[i].Name << " " << p[i].Age << "/" << p[i].Gpa;
	}
	cout << endl;
	
	// Have all data under variable Name uppercase 
	

}

void Toupper()
{
	
	for (int i = 0; i < N; i++)
	{
		 _strupr(p[i].Name);
	}
	
	
	//Display the data all uppercase
	Display();
}
char Name[15]; > its for max 14 char

use a loop , untill find "/" input then in name
then input age
then gpa
Sorry but I do not understand what you are saying

I cannot INPUT anything, I have to read the data from the file not input my own data...
use vector to store each new line.

if you dont know vector, use pointers > count how many lines are there. and
RECORD rc = new RECORD [totallines];
I cannot use vectors nor can I use pointers.

Let me rephrase my question...

Is there something wrong in this function? Does this not gather all the data from the text file?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

void CopyRecords(string Fname)
{


	fstream f;
	f.open(Fname, ios::in);

	for (int i = 0; i < N; i++)
	{

		f.getline(p[i].Name, 15, '/');
		f >> p[i].Age >> p[i].Gpa;
	}

	f.close();
}


Is there something wrong in this function? Does this display all of the information stored from the structure?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
void Display()
{
	
	
	for (int i = 0; i < N; i++)
	{
		cout << p[i].Name << " " << p[i].Age << "/" << p[i].Gpa;
	}
	cout << endl;
	
	// Have all data under variable Name uppercase 
	

}


you want to copy data from text file to a structure. for this you have to create structure objets. how many objects? equal to lines of file (==names)

if name numbers are fixed,
you can, RECORD rec[nameNumber];

but if its variable you have to use pointers or vector.

I cannot INPUT anything

i mean input from file, to your program.
Ok can you answer my previous question
why don't you test yourself like this:
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
#include <iostream>
#include <fstream>
using namespace std;

const int N = 5;

struct RECORD
{
	char Name[15];
	int Age;
	float Gpa;
};

RECORD p[N]; 
float GpaAve;


int main ()
{

	ifstream f;
	in.open("fileName.txt");
	
	for (int i = 0; i < 5; i++)
	{
		f.getline(p[i].Name, 15, '/'); 
		f >> p[i].Age >> p[i].Gpa;
	}
	
	for (int i = 0; i < 5; i++)
	{
	cout << "name " << p[i].Name << " age " <<p[i].Age << " gpa " << p[i].Gpa << endl; // does it show the desired?
	}	
	
	
return 0;	
}
Last edited on
what is the difference between ifstream f; and fstream f;

and why in.open?

text file:
5
22 2.2 Bill Clinton
33 3.3 Phil Jackson
44 4.0 Mary A Johnson
25 2.5 Adam Smith
65 3.2 George W Bush

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
#include <iostream>
#include <fstream>
#include <array>

using namespace std;

struct PERSON
{
	int age;
	float gpa;
	char name[20];
};
PERSON *p;

void ArraySize(string Fname, int &x);
void CopyData(string Fname, int x);
void DisplayData(int x);

int main()
{
	int size;
	
	ArraySize("two.txt", size); //determine dnynamic array size
	
	cout << "The size of the array is " << size << endl;

	CopyData("two.txt", size); //copy data from file to array p

	DisplayData(size); //Display the data

	system("PAUSE");
	return 0;


}

void ArraySize(string Fname, int  &x)
{
	int n;
	fstream f;
	f.open(Fname, ios::in);
	f >> n;

	x = n;

	f.close();

}

void CopyData(string Fname, int x)
{
	ifstream f;
	f.open(Fname, ios::in);

	for (int i = 0; i < x; i++)
	{
		f >> p[i].age; 
		f >> p[i].gpa;
		f.getline(p[i].name, 20, '\n');
	}

	f.close();
}

void DisplayData(int x)
{
	for (int i = 0; i < x; i++)
	{
		cout << p[i].age << " " << p[i].gpa << " " << p[i].name << endl;
	}
}


Why doesn't this work?

A window pops up saying: "Unhandled exception at x61D735BA (msvcp120d.dll) in Project7(2).exe: 0xC0000005: Access violation writing location 0x00000000.
Last edited on
this worked fine in my pc
inf.txt:
5
22 2.2 Bill Clinton
33 3.3 Phil Jackson
44 4.0 Mary A Johnson
25 2.5 Adam Smith
65 3.2 George W Bush
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
#include <iostream>
#include <fstream>
using namespace std;

struct PERSON
{
	char Name[50];
	int Age;
	float Gpa;
};
 
float GpaAve;


int main ()
{
	PERSON *ptr;
	int lines;

	ifstream f("inf.txt");
	f >> lines;
	ptr = new PERSON [lines];
	
	for (int i = 0; i < lines; i++)
	{		 
		f >> ptr[i].Age;
		f >> ptr[i].Gpa;
		f.getline(ptr[i].Name, 50, '\n');
	}
	
	for (int i = 0; i < lines; i++)	
	cout << "name: " << ptr[i].Name << " age: " <<ptr[i].Age << " gpa: " << ptr[i].Gpa << endl;	
	
return 0;
}
Last edited on
You get the array size but never actually create the array.

p = new PERSON[size];

Then just don't forget to delete what you new before the end of main.

delete[] p;
mobotus wrote:
You get the array size but never actually create the array.

p = new PERSON[size];

Then just don't forget to delete what you new before the end of main.

delete[] p;


don't you see i used them between line 24 and 29.

i am not writing everything along the way as an assignment - rather showing whats needed. and, when main ends it automatically delets the pointer - its a small program.
@anup30 I was referring to the OP's code.
okay
Thank you very much, I can't believe I overlooked something that important when dealing with dynamic arrays.

One last question, why do you use ifstream f ("text file")? What is the difference between that and;

fstream f;
f.open("text file", ios::in)

fstream input and output
ifstream input
ofstream output
Why wouldn't you just use fstream then if it's more powerful?
fstream is summation of ifstream and ofstream.

its similar to many people learn C, while C++ gives more opportunity.
Same reason why you make some things const. to keep it safe. If you have data you don't want to over-write create an input stream. Then if you try to do something silly the compiler will yell at you.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <fstream>
#include <string>

int main(){

	std::string name;
	std::ifstream file;

	file.open("someFile");

	file >> name; //OK
	file << name; //Compile fails

}

or, why do you keep your 100$ note in money bag, while A briefcase gives you more space?
Pages: 12