Data missing in variable

This is a discussion on Data missing in variable within the C Programming forums, part of the General Programming Boards category; I'm willing to bet everyone is sick of me . Data is missing or being passed over for the variable ...

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

    Data missing in variable

    I'm willing to bet everyone is sick of me . Data is missing or being passed over for the variable s->curr. I'm sending all the data to an output file and don't see s->curr being printed. I'm reading an input file where the data is seperated by colons. Heres my code:
    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);
         fprintf(out, "Student Name                 SSN           DOB         Curr      Grad Yr\n");
         fprintf(out, "------------                 ---           ---         ----      -------\n");
         
         while(! feof(in)) {
                 
              p = buf;
              x = strchr(p,':');
              *x = '\0';
              strcpy(s->name, p);
              p = x + 1;
              x = strchr(p,':');
              strcpy(s->curr, p);
              p = x + 1;
              x = strchr(p,':');
              s->gradyr = atoi(p);
              p = x + 1;
              x = strchr(p,':');
    		s->ssn = atoi(p);
    		p = x + 1;
    		x = strchr(p,':');
    		strcpy(s->dob, p);
              p = x + 1;
              x = strchr(p,':');
              fprintf(out, "%-25s", 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, "%s", 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;     
    }

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    > x = strchr(p,':');
    > strcpy(s->curr, p);

    > x = strchr(p,':');
    > strcpy(s->dob, p);

    You're not terminating your strings before you use strcpy().
    Code:
              x = strchr(p,':');
              *x = '\0';
              strcpy(s->curr, p);
    
    		x = strchr(p,':');
    		*x = '\0';
    		strcpy(s->dob, p);
    Also s->curr only has room for 3 characters, plus the string terminator, so make sure it's only three characters long, or declare it bigger.

  3. #3
    Registered User
    Join Date
    Oct 2004
    Posts
    76
    I add the *x = '\0'; before the strcpy and I get a coredump.

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,484
    Paste
    - your latest code
    - some example data in your file.
    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.

  5. #5
    Registered User
    Join Date
    Oct 2004
    Posts
    76
    My latest code, which I don't think has changed from the first post:
    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);
         fprintf(out, "Student Name                 SSN           DOB         Curr      Grad Yr\n");
         fprintf(out, "------------                 ---           ---         ----      -------\n");
         
         while(! feof(in)) {
                 
              p = buf;
              x = strchr(p,':');
              *x = '\0';
              strcpy(s->name, p);
              p = x + 1;
              x = strchr(p,':');
              strcpy(s->curr, p);
              p = x + 1;
              x = strchr(p,':');
              s->gradyr = atoi(p);
              p = x + 1;
              x = strchr(p,':');
    		s->ssn = atoi(p);
    		p = x + 1;
    		x = strchr(p,':');
    		strcpy(s->dob, p);
              p = x + 1;
              x = strchr(p,':');
              fprintf(out, "%-25s", 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, "%s", 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;     
    }
    Some example data:
    Code:
    Tim Smith:ACT:2008:451237458:04-15-1981
    Jen Hoover:NUR:2007:895621478:11-06-1979
    Tim Schneebly:MUS:2006:123054697:12-25-1974
    Tina Rimmer:SCI:2010:756213025:03-14-1983
    Fred Rogers:REL:2011:456158743:05-13-1985

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,484
    > *x = '\0';
    You're missing all of these after the first field.

    > x = strchr(p,':');
    > strcpy(s->dob, p);
    Looping when outputting to a file
    Note that your last field ends with '\n', not ':'
    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.

  7. #7
    Registered User
    Join Date
    Oct 2004
    Posts
    76
    Adding *x = '\0'; coredumps. The last field ends with '\n' doesn't explain why s->curr is not getting its data.

  8. #8
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,484
    If strchr doesn't find what it's looking for, it returns NULL, and doing *x = '\0'; on such a result is bad news.
    Are you sure all your input data is valid?

    Well looking at your code, you have one too many strchr calls.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    struct stuinfo {
        char name[30];
        int ssn;
        char dob[11];
        char curr[4];
        int gradyr;
    };
    
    void decode(char *buf)
    {
        FILE *out = stdout;
        char *p, *x;
        struct stuinfo stu;
        struct stuinfo *s = &stu;
    
        p = buf;
        x = strchr(p, ':');
        *x = '\0';
        strcpy(s->name, p);
    
        p = x + 1;
        x = strchr(p, ':');
        *x = '\0';
        strcpy(s->curr, p);
    
        p = x + 1;
        x = strchr(p, ':');
        *x = '\0';
        s->gradyr = atoi(p);
    
        p = x + 1;
        x = strchr(p, ':');
        *x = '\0';
        s->ssn = atoi(p);
    
        p = x + 1;
        x = strchr(p, '\n');
        *x = '\0';
        strcpy(s->dob, p);
    
        fprintf(out, "%-25s", s->name);
        fprintf(out, "%-3d-%2d-%4d", s->ssn / 1000000, (s->ssn / 10000) % 100,
                s->ssn % 10000);
        fprintf(out, "   %s", s->dob);
        fprintf(out, "   %s", s->curr);
        fprintf(out, "   %d\n", s->gradyr);
    }
    
    
    int main()
    {
        char *tests[] = {
            "Tim Smith:ACT:2008:451237458:04-15-1981\n",
            "Jen Hoover:NUR:2007:895621478:11-06-1979\n",
            "Tim Schneebly:MUS:2006:123054697:12-25-1974\n",
            "Tina Rimmer:SCI:2010:756213025:03-14-1983\n",
            "Fred Rogers:REL:2011:456158743:05-13-1985\n",
        };
        size_t i;
        for ( i = 0 ; i < sizeof(tests)/sizeof(*tests); i++ ) {
            char temp[100];
            strcpy( temp, tests[i] );
            decode( temp );
        }
        return 0;
    }
    My results
    Code:
    Tim Smith                451-23-7458   04-15-1981   ACT   2008
    Jen Hoover               895-62-1478   11-06-1979   NUR   2007
    Tim Schneebly            123- 5-4697   12-25-1974   MUS   2006
    Tina Rimmer              756-21-3025   03-14-1983   SCI   2010
    Fred Rogers              456-15-8743   05-13-1985   REL   2011
    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.

  9. #9
    Registered User
    Join Date
    Oct 2004
    Posts
    76
    I stepped a way for while and then it hit me like a ton of brick as what you were saying about the data ending with a '\n'. I thank you for your patience and help.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. more then 100errors in header
    By hallo007 in forum Windows Programming
    Replies: 20
    Last Post: 05-13-2007, 08:26 AM
  2. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  3. creating class, and linking files
    By JCK in forum C++ Programming
    Replies: 12
    Last Post: 12-08-2002, 01:45 PM
  4. C Programming Question
    By TK in forum A Brief History of Cprogramming.com
    Replies: 13
    Last Post: 07-04-2002, 07:11 PM

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