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
|
/*
This programs takes input through ioredirection with the structure of
name
address
citystate
zip
and arranges them by zipcode
MAKNELTEK
5/26/2009
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
const int TOTAL = 18;
typedef struct info {
char name[30];
char address[30];
char citystate[30];
char zip[30];
}a;
void get_info(struct info *a, FILE *fn);
void sortdata( struct info *a);
void write_out(struct info *a, FILE *fo);
char text[100], text2[100];
void main(void)
{
printf ("Enter a filename to load: ");
scanf ("%s" , &text);
printf ("Enter output file name: ");
scanf ("%s" , &text2);
FILE *fn, *fo;
fn = fopen(text, "r");
if (fn == NULL) /*error checking*/
{
printf ("Cannot open file %s \n", text);
exit (0);
}
fo = fopen(text2, "w");
if (fo == NULL) /*error checking*/
{
printf ("Cannot open file %s", text2);
}
info *spc;
spc = (info*)malloc (sizeof(info));
get_info(spc, fn);
sortdata(spc);
write_out(spc, fo);
fclose (fn);
fclose (fo);
}
void get_info(info *a, FILE *fn) /*pulls all info*/
{
while ( !feof(fn))
{
char *p;
for ( int i = 0; i <TOTAL; i++){
fgets(a[i].name, sizeof(a[i].name), fn); /*records name*/
if ((p = strchr(a[i].name, '\n')) != (NULL))
*p = '\0';
fgets (a[i].address, sizeof(a[i].address), fn); /*records address*/
if ((p = strchr(a[i].address, '\n')) != (NULL))
*p = '\0';
fgets (a[i].citystate, sizeof(a[i].citystate), fn); /*records city state*/
if ((p = strchr(a[i].citystate, '\n')) != (NULL))
*p = '\0';
fgets (a[i].zip, sizeof(a[i].zip),fn); /*record zip*/
if ((p = strchr(a[i].zip, '\n')) != (NULL))
*p = '\0';
}
}
}
void write_out(struct info *a, FILE *fo) /*Writes out to file*/
{
for ( int i = 0; i <TOTAL; i++)
{
fputs(a[i].name, fo);
fputc ('\n', fo );
fputs (a[i].address, fo);
fputc ('\n', fo );
fputs (a[i].citystate, fo);
fputc ('\n', fo );
fputs (a[i].zip,fo);
fputc ('\n', fo );
}
}
void sortdata(info *a)
{
int t;
for (t=0; t<TOTAL; t++)
{
int i;
for ( i = t; i < TOTAL; i++)
{
if (-1==(strcmp(a[i].zip,a[t].zip)))
{ /*swaps entire data*/
char temp[30];
memcpy(temp, a[i].zip, sizeof(a[i].zip)); /*swaps zip*/
memcpy(a[i].zip, a[t].zip, sizeof(a[t].zip));
memcpy(a[t].zip, temp, sizeof(temp));
memcpy(temp, a[i].name, sizeof(a[i].name)); /*swaps name*/
memcpy(a[i].name, a[t].name, sizeof(a[t].name));
memcpy(a[t].name, temp, sizeof(temp));
memcpy(temp, a[i].address, sizeof(a[i].address)); /*swaps address*/
memcpy(a[i].address, a[t].address, sizeof(a[t].address));
memcpy(a[t].address, temp, sizeof(temp));
memcpy(temp, a[i].citystate, sizeof(a[i].citystate)); /*swaps city-state*/
memcpy(a[i].citystate, a[t].citystate, sizeof(a[t].citystate));
memcpy(a[t].citystate, temp, sizeof(temp));
}
}
}
}
|