I modified my code to what you suggested and my output file looks like this:
Code:
0 - 0- 0 0
0 - 0- 0 0
0 - 0- 0 0
0 - 0- 0 0
0 - 0- 0 0
This is my program with the changes:
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;
while(x != NULL) {
x = strchr(p,':');
*x = '\0';
strcpy(s->name, p);
x = strchr(p,':');
*x = '\0';
strcpy(s->curr, p);
x = strchr(p,':');
*x = '\0';
s->gradyr = atoi(p);
x = strchr(p,':');
*x = '\0';
s->ssn = atoi(p);
x = strchr(p,':');
*x = '\0';
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;
}