I have a project that is due and I have written the code and for a part of the code output to the file I cannot get the right format. I cannot figure out what the formatting should be can someone please help me.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
struct stuinfo {
char name[30];
char dob[11];
char curr[4];
int gradyr;
int ssn;
};
int main(int argc, char **argv){
char buf[80],ers[200]; /* file buffer area*/
FILE *in; /* input file stream pointer */
FILE *out; /* output file stream pointer*/
char *c, *p;
int ssn, f,l,m;
struct stuinfo *s;
in=fopen(argv[1], "r");
if(in == NULL){
sprintf(ers,"filename: %s Action: fopen Error:\n", argv[1]);
perror(ers);
exit(1);
}
out=fopen(argv[2], "w");
if(out == NULL){
sprintf(ers,"filename: %s Action: fopen Error:\n", argv[2]);
perror(ers);
fclose(in);
exit(2);
}
s = malloc(sizeof(struct stuinfo));
if(s == NULL)
exit(1);
fprintf("Student Name SSN DOB CURR GRAD Yr");
while(fgets(buf,80,in)!=NULL){
fprintf(out, "%s", buf);
p = buf;
c = strchr(p,':');
*c = '\0';
strcpy(s -> name, p);
p= c+1;
c = strchr(p,':');
*c = '\0';
strcpy(s -> curr, p);
p= c+1;
c = strchr(p,':');
*c = '\0';
s-> gradyr = atoi( p);
p= c+1;
c = strchr(p,':');
*c = '\0';
s->ssn = atoi( p);
f = ssn/1000000;
ssn=ssn%1000000;
m=ssn/10000;
l=ssn%10000;
p= c+1;
c = strchr(p,'\n');
*c = '\0';
strcpy(s -> dob, p);
p= c+1;
fprintf(out,"%-30s\n %03d-%02d-4d\n %15s\n %8s\n %8d\n", s->name,f,m,l,s->dob,s->curr,s->gradyr);
}
fclose(out);
fclose(in);
free(s);
}
the full formatted print out should look like this:
Student Name SSN DOB Curr Grad Yr
John Jones 123-45-6789 04-19-1978 CIS 2009
I cannot get the SSN to print out with the dashes nor can I get the heading to print. Can someone help me?
Thank you.:sad: