Read file in, alphabetize and put into a new file

I have no idea where to really start on this program. I have to read in an existing file that is first name, last name, program, and number under that division. How would I go about reading that in, alphabetizing it by last name and putting it into another file? I have not done much with file access and string compares.

Thank you for all the help!
to get you thinking,
i would use string functions..i did a program like this awhile ago.
i used substr and stored everything in that line into an array, by section;
e.g. john smith 10
0123 5-10 12-14
while( != eof)
store each line into an array..the size depends on how the format of the file is
getline(0-14)
string firstName=substr(0-3)
string lastName=substr(5-10)
stoi(turns string into int)

..look up how to use substr but keep in mind there are several way to do this;
but once you get the information stored everything else is pretty simple
Hope this helps
Last edited on
I have to alphabetize each row now.. which I am not too sure how to do.
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
#include "stdafx.h"

struct filetype
{
	char fname[20];
	char lname[20];
	char cname[20];
	int students;

};

int _tmain(int argc, _TCHAR* argv[])
{
	FILE *ptr;
	errno_t Error;
	struct filetype files[15];


	Error = fopen_s(&ptr, "names.txt", "r");
	if (Error)
	{
		printf("Can't open file\n");
		return 0;
	}
	//ifstream theFile("names.txt");


	int i = 0;

	while (!feof(ptr))
	{

	
		fscanf_s(ptr, "%s %s %s %d\n", files[i].fname, sizeof(files[0].fname), files[i].lname, sizeof(files[i].lname), files[i].cname, sizeof(files[i].cname), &files[i].students);
		printf("%s %s %s %d\n", files[i].fname,  files[i].lname,  files[i].cname, files[i].students);

		i++;
	



	}

	fclose(ptr);

	return 0;
}
Last edited on
it would help if we could see the file that has to be read in and what the outfile should look like
So I have more, but still unable to get it to alphabetize it.
This is the file I have to read in:

Gary Todd EEET321 18
Murry Stocking EEET114 21
Robert Most EEET422 17
Ronald Mehringer EEET124 19
Warren Klope ECNS322 15
Jeff Pedelty EEET122 20
Keith Jewett ECNS315 10
Steve Johnson ECNS311 19


Here is what my code currently 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
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

struct filetype
{
	char fname[20];
	char lname[20];
	char cname[20];
	char students[20];

};

int _tmain(int argc, _TCHAR* argv[])
{
	FILE *ptr;
	errno_t Error;
	struct filetype files[15],temp;


	Error = fopen_s(&ptr, "names.txt", "r");
	if (Error)
	{
		printf("Can't open file\n");
		return 0;
	}
	//ifstream theFile("names.txt");


	int i = 0;

	while (!feof(ptr))
	{

	
		fscanf_s(ptr, "%s %s %s %s\n", files[i].fname, sizeof(files[0].fname), files[i].lname, sizeof(files[i].lname), files[i].cname, sizeof(files[i].cname), files[i].students, sizeof(files[i].students));
		printf("%s %s %s %s\n", files[i].fname,  files[i].lname,  files[i].cname, files[i].students);

		i++;
	}
	int b = 0;
	int c = 1;
	int compare;
	cout << endl;
	cout << "These are the alphabatized people" << endl;
	cout << endl;
	int swapInt = 0;
	for (b = 0; b < i - 1; b++, c++)
	{
		compare = strcmp(files[b].lname, files[b+1].lname);

		if (compare < 0);
		{
			strcpy_s(temp.fname, sizeof(temp.fname), files[b].fname);
			strcpy_s(temp.lname, sizeof(temp.lname), files[b].lname);
			strcpy_s(temp.cname, sizeof(temp.cname), files[b].cname);
			strcpy_s(temp.students, sizeof(temp.students), files[b].students);


			strcpy_s(files[b].fname, sizeof(files[b].fname), files[b+1].fname);
			strcpy_s(files[b].lname, sizeof(files[b].lname), files[b+1].lname);
			strcpy_s(files[b].cname, sizeof(files[b].cname), files[b+1].cname);
			strcpy_s(files[b].students, sizeof(files[b].students), files[b+1].students);


			strcpy_s(files[b+1].fname, sizeof(files[b+1].fname), temp.fname);
			strcpy_s(files[b+1].lname, sizeof(files[b+1].lname), temp.lname);
			strcpy_s(files[b+1].cname, sizeof(files[b+1].cname), temp.cname);
			strcpy_s(files[b+1].students, sizeof(files[b+1].students), temp.students);



			printf("%s %s %s %s\n", files[b].fname, files[b].lname, files[b].cname, files[b].students);

		}
		ofstream outFile;
		outFile.open("outFile.txt");
		//int x;
		//for (x = 0; x < i; x++);
		//{
		outFile << files[0].fname << " " << files[0].lname << " " << files[0].cname << " " << files[0].students << endl;
		outFile << files[1].fname << " " << files[1].lname << " " << files[1].cname << " " << files[1].students << endl;
		outFile << files[2].fname << " " << files[2].lname << " " << files[2].cname << " " << files[2].students << endl;
		outFile << files[3].fname << " " << files[3].lname << " " << files[3].cname << " " << files[3].students << endl;
		outFile << files[4].fname << " " << files[4].lname << " " << files[4].cname << " " << files[4].students << endl;
		outFile << files[5].fname << " " << files[5].lname << " " << files[5].cname << " " << files[5].students << endl;
		outFile << files[6].fname << " " << files[6].lname << " " << files[6].cname << " " << files[6].students << endl;
		outFile << files[7].fname << " " << files[7].lname << " " << files[7].cname << " " << files[7].students << endl;
		int z = 1;
		//}
		
		outFile.close();
	}

	fclose(ptr);

	return 0;
}
Im not too certain if your using c++ but the concept should be the same...(I do not understand your code)
1. i would have a loop until end of file
2.that reads in each line at a time in c++ e.g getline
3. store everything in an char array e.g
4. use a substr function to store the first name/last name/ blah blah
5. once it is stored you use if conditions to compare largest to smallest to get into alphabetical order

if you are allowed to use classes that would help ALLOT and you would have an array object:
class array[25] that you store everything in. for each array you would store the first/lastname//etc.
Topic archived. No new replies allowed.