My latest code, which I don't think has changed from the first post:
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main () {
struct stuinfo {
char name[30];
int ssn;
char dob[11];
char curr[4];
int gradyr;
};
int y;
char buf[80], *p, *x; /* file buffer area */
FILE *in; /* input file stream pointer */
FILE *out; /* output file stream pointer */
struct stuinfo *s;
s = malloc(sizeof(struct stuinfo));
in = fopen("d6.dat", "r");
if(in == NULL) {
perror("fopen: d6.dat");
exit(1);
}
out = fopen("d6.out", "w");
if(out == NULL) {
perror("fopen: d6.out");
exit(1);
}
fgets(buf, 80, in);
fprintf(out, "Student Name SSN DOB Curr Grad Yr\n");
fprintf(out, "------------ --- --- ---- -------\n");
while(! feof(in)) {
p = buf;
x = strchr(p,':');
*x = '\0';
strcpy(s->name, p);
p = x + 1;
x = strchr(p,':');
strcpy(s->curr, p);
p = x + 1;
x = strchr(p,':');
s->gradyr = atoi(p);
p = x + 1;
x = strchr(p,':');
s->ssn = atoi(p);
p = x + 1;
x = strchr(p,':');
strcpy(s->dob, p);
p = x + 1;
x = strchr(p,':');
fprintf(out, "%-25s", s->name);
fprintf(out, "%-3d-%2d-%4d", s->ssn / 1000000, (s->ssn / 10000) % 100, s->ssn % 10000);
for(y = 0;y < 3;y++)
fputc(' ', out);
fputs(s->dob, out);
fprintf(out, "%s", s->curr);
fprintf(out, "%d\n", s->gradyr);
fgets(buf, 80, in);
}
fclose(out);
fclose(in);
free(s);
printf ("Press ENTER to continue.\n");
getchar ();
return 0;
}
Some example data:
Code:
Tim Smith:ACT:2008:451237458:04-15-1981
Jen Hoover:NUR:2007:895621478:11-06-1979
Tim Schneebly:MUS:2006:123054697:12-25-1974
Tina Rimmer:SCI:2010:756213025:03-14-1983
Fred Rogers:REL:2011:456158743:05-13-1985