Looping when outputting to a file

This is a discussion on Looping when outputting to a file within the C Programming forums, part of the General Programming Boards category; I'm sure its a problem of where the statement are located in my while loop, but all that gets inputted ...

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

    Looping when outputting to a file

    I'm sure its a problem of where the statement are located in my while loop, but all that gets inputted in my ouput file is the persons ssn. Now the ssn is on the line it should be. The input file's data is seperated by colons, which I'm using to sort the data. The input file looks like this:
    Code:
    name:major:gradyr:social:dob
    Then once the data is sorted, it is sent to a output file under the appropriate column like this:
    Code:
    name        social      dob       major       gradyr
    -------        -------      -----      --------      ---------
    This is my program:
    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);
         
         while(! feof(in)) {
                 
              p = buf;
              x = strchr(p,':');
              
              while(x != NULL) {
              
                   *x = '\0';
                   strcpy(s->name, p);
                   strcpy(s->curr, p);
                   s->gradyr = atoi(p);
    		     s->ssn = atoi(p);
    		     strcpy(s->dob, p);
                   p = x + 1;
                   x = strchr(p,':');
              }
              
              fprintf(out, "%s", 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, "%-4s", 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
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,824
    strcpy(s->name, p);
    strcpy(s->curr, p);
    s->gradyr = atoi(p);
    s->ssn = atoi(p);
    strcpy(s->dob, p);

    You're copying the same thing to all 5 fields each time around the loop.

    This should be
    Code:
    x = strchr(p,':');
    *x = '\0';
    strcpy(s->name, p);
    x = strchr(p,':');
    *x = '\0';
    strcpy(s->curr, p);
    // etc
    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.

  3. #3
    Registered User
    Join Date
    Oct 2004
    Posts
    76
    I modified my code to what you suggested and my output file looks like this:
    Code:
    0  - 0-   0       0
    0  - 0-   0       0
    0  - 0-   0       0
    0  - 0-   0       0
    0  - 0-   0       0
    This is my program with the changes:
    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);
         
         while(! feof(in)) {
                 
              p = buf;
              
              while(x != NULL) {
              
                   x = strchr(p,':');
                   *x = '\0';
                   strcpy(s->name, p);
                   x = strchr(p,':');
                   *x = '\0';
                   strcpy(s->curr, p);
                   x = strchr(p,':');
                   *x = '\0';
                   s->gradyr = atoi(p);
                   x = strchr(p,':');
                   *x = '\0';
    		     s->ssn = atoi(p);
    		     x = strchr(p,':');
                   *x = '\0';
    		     strcpy(s->dob, p);
                   p = x + 1;
                   x = strchr(p,':');
              }
              
              fprintf(out, "%s", 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, "%-4s", 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;     
    }

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,824
    Yeah, because *x = '\0'; and a strcpy is just a null string

    Code:
    char *front = buff, *back;
    
    back = strchr( front, ':'); *back = '\0';  // find a :, set it to \0
    strcpy( strcpy(s->name, front);  // copy
    front = back + 1;  // one char beyond
    
    back = strchr( front, ':'); *back = '\0';  // find a :, set it to \0
    strcpy( strcpy(s->curr, front);  // copy
    front = back + 1;  // one char beyond
    Note that your last field ends with '\n', not ':'

    > while(x != NULL) {
    There's no need for this.
    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
    I got the code working but I'm missing the curr value. Its missing from the output file.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A development process
    By Noir in forum C Programming
    Replies: 37
    Last Post: 07-10-2011, 11:39 PM
  2. Newbie homework help
    By fossage in forum C Programming
    Replies: 3
    Last Post: 04-30-2009, 05:27 PM
  3. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  4. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 11:12 PM
  5. Hmm....help me take a look at this: File Encryptor
    By heljy in forum C Programming
    Replies: 3
    Last Post: 03-23-2002, 10:57 AM

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