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;
}
|