Output file contains extra data

This is a discussion on Output file contains extra data within the C Programming forums, part of the General Programming Boards category; When I look at my output file to my program the ssn has the first 3 numbers printed, then the ...

  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.

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,755
    > 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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  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, 07:39 AM
  2. Encryption program
    By zeiffelz in forum C Programming
    Replies: 1
    Last Post: 06-15-2005, 04: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, 10:54 AM
  5. HUGE fps jump
    By DavidP in forum Game Programming
    Replies: 23
    Last Post: 07-01-2004, 11:36 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21