When I look at my output file to my program the ssn has the first 3 numbers printed, then the whole social printed after it so it looks like:
I can't see where I went wrong. Here is what i got:
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;
};
char buf[80]; /* 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);
}
fprintf(out, "Student Name SSN DOB Curr Grad Yr\n");
fprintf(out, "------------ --- --- ---- -------\n");
while( fgets(buf,sizeof(buf),in) != 0) {
strcpy(s->name,strtok(buf,":"));
strcpy(s->curr,strtok(0,":"));
s->gradyr = atoi(strtok(0,":"));
s->ssn = atoi(strtok(0,":"));
strcpy(s->dob,strtok(0,":"));
fputs(s->name, out);
fprintf(out, "%3d-%2d-%4d", s->ssn / 1000000, s->ssn / 10000, s->ssn % 10000);
fputs(s->dob, out);
fputs(s->curr, out);
fprintf(out, "%6d\n", s->gradyr);
printf("%s\n%s\n%d\n%d\n%s\n", s->name, s->curr, s->gradyr, s->ssn, s->dob);
}
fclose(out);
fclose(in);
printf ("Press ENTER to continue.\n");
getchar ();
return 0;
}