Thread: Output file contains extra data

  1. #1
    Registered User
    Join Date
    Oct 2004
    Posts
    76

    Output file contains extra data

    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:
    Code:
    234-24367-8765
    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;     
    }

  2. #2
    Registered User caroundw5h's Avatar
    Join Date
    Oct 2003
    Posts
    751
    And what is the exact output your expecting? It'd be helpful to know so you can get some help.
    Warning: Opinions subject to change without notice

    The C Library Reference Guide
    Understand the fundamentals
    Then have some more fun

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > s->ssn / 10000
    How about
    (s->ssn / 10000) % 100

    If you're just trying to extract two digits from it.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Oct 2004
    Posts
    76
    Thanks!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  2. Encryption program
    By zeiffelz in forum C Programming
    Replies: 1
    Last Post: 06-15-2005, 03:39 AM
  3. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 10:46 AM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. HUGE fps jump
    By DavidP in forum Game Programming
    Replies: 23
    Last Post: 07-01-2004, 10:36 AM