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:

Recommended Answers

All 5 Replies

fprintf("Student Name SSN DOB CURR GRAD Yr");

Did you forget where you want the be printed?.
fprintf(out,"Student Name SSN DOB CURR GRAD Yr");

Also

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);

If you don't take away the \n after every format operator you will have this:

John Jones
123-45-6789
04-19-1978
CIS
2009

fprintf(out,"%-30s %03d-%02d-%4d %15s %8s %8d\n", s->name,f,m,l,s->dob,s->curr,s->gradyr);

Thank you I forgot the out in the statement and to put in the while loop. Now to tackle why the ssn is not printing the way it should.

Can anyone help with that?:

Now to tackle why the ssn is not printing the way it should.

Could you post what is it that prints?.

Did you notice in my previous post the little correction I did to the last fprintf?. You were missing a
% between %2d and 4d I think.

I noticedthat but I am still not getting the correct print out , I am copying what I get for the print out ans you can see that the ssn is not correct:
Student Name SSN DOB Curr Grad Yr
Jim Jones 000-00- 0 03-12-1956 ELT 2005
Philly Johns 000-00- 0 12-15-1989 CIS 2003
William Sweeney 000-00- 0 01-03-1990 MAS 2006
Terry Hundred 000-00- 0 04-15-1965 BAD 2001
Susan Pine 000-00- 0 08-08-1988 INS 2007

The reason that the social security number is not shown, is because
your program is protecting you from showing to everyone in
Daniweb, the social of so many people. :cheesy: You have build-in encription in your program. I'm joking!.

I can see that the print format is correct now. So the pro
blem is that f, m, and l, is not recieving the correct information

s->ssn = atoi( p);
f = ssn/1000000;
ssn=ssn%1000000;
m=ssn/10000;
l=ssn%10000;

What is the value store in f, m, and l if ssn doesn't contain
the information that you think?. What is in ssn before you try to
divide the digits?.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.