I'm sure its a problem of where the statement are located in my while loop, but all that gets inputted in my ouput file is the persons ssn. Now the ssn is on the line it should be. The input file's data is seperated by colons, which I'm using to sort the data. The input file looks like this:
Then once the data is sorted, it is sent to a output file under the appropriate column like this:Code:name:major:gradyr:social:dob
This is my program:Code:name social dob major gradyr ------- ------- ----- -------- ---------
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); while(! feof(in)) { p = buf; x = strchr(p,':'); while(x != NULL) { *x = '\0'; strcpy(s->name, p); strcpy(s->curr, p); s->gradyr = atoi(p); s->ssn = atoi(p); strcpy(s->dob, p); p = x + 1; x = strchr(p,':'); } fprintf(out, "%s", 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, "%-4s", 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; }



LinkBack URL
About LinkBacks


