Help on my code please.

This is a discussion on Help on my code please. within the C Programming forums, part of the General Programming Boards category; I am currently trying on this code, and I want the output to be the same as the input, but ...

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    14

    Help on my code please.

    I am currently trying on this code, and I want the output to be the same as the input, but my code fails to do that. Please help me out.

    Code:
    /*
     * A sample program to read-from and write-to files. Also shows how to
     * send a file as a parameter and display error messages to standard
     * error.
     */
    #include "test.h"
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <conio.h>
    
    
    
    int main () {
        STUDENT stuArr[MAX_STUDENTS];
        void rmNl ( char s[] );
        void *fin, *fout;
        void grades_in(FILE *fin, FILE *fout, STUDENT stuArr[MAX_STUDENTS]);
        
       /* Open file to read input data */
       if ( ( fin = fopen ( "grades.in", "r" ) ) == NULL ) {
          fprintf ( stderr, "Could not open file grades.in\n" );
          exit ( -1 );
       }
    
       /* Open file for writing output */
       if ( ( fout = fopen ( "grades.out", "w" ) ) == NULL ) {
          fprintf ( stderr, "Could not open file grades.out\n" );
          fclose ( fin );
          exit ( -1 );
       }
       grades_in(fin, fout, stuArr);
       system ("PAUSE");    
       return 0;
    }
       
       
    void grades_in(FILE *fin, FILE *fout, STUDENT stuArr[MAX_STUDENTS]) {
       int numItems = 0;
       char buf[MAX_STUDENTS];  
       char fname[50], lname[50];
       int h1, h2, h3, h4, h5, e1, e2, e3, id;  
       while( fgets ( buf, MAX_STUDENTS, fin ) != NULL ) {
                rmNl ( buf );
                if ( !lineWithBlanks ( buf ) )
                if ( sscanf(buf, "%s %s", fname, lname) == 2 ) printf ("%s %s\n", fname, lname);
                if ( sscanf(buf, "%d", &id) == 1 ) printf ("%d\n", id);
                if ( sscanf(buf, "%d %d %d %d %d", &h1, &h2, &h3, &h4, &h5) == 5 ) printf ("%d %d %d %d %d\n", h1, h2, h3, h4, h5); 
                if ( sscanf(buf, "%d %d %d", &e1, &e2, &e3) == 3 ) printf ("%d %d %d\n", e1, e2, e3);
                numItems++;
                continue; 
    
    
       }
       
       
       
       
    }      
    
    
    
    
    
    
    
    
    
    
    
    
    /*
     * Function to remove new line character
     */
    void rmNl ( char str[] )
    {
       char *ptr;
    
       if ( ( ptr = strchr ( str, '\n' ) ) )
          *ptr = '\0';
    }
            
    
    
    /*
     * Check to see if line is an empty one with only spaces
     * Returns 1 if line consists of blank spaces, 0 otherwise
     */
    int lineWithBlanks ( char *s )
    {
       int result = 1, i;
    
       for ( i = 0; i < strlen (s); i++ ) {
          if ( s[i] != ' ' ) {
             result = 0;
             break;
          }
       }
       return result;
    }


    my input data looks like this:

    John Doe
    1111
    98 87 69 97 89
    85 91 88
    Bill Doe
    2222
    77 78 87 79 85
    69 73 89

  2. #2
    Registered User
    Join Date
    Feb 2006
    Posts
    43
    Well....from what I can see you've got a couple problems,
    Code:
    int main () {
        STUDENT stuArr[MAX_STUDENTS]; STUDENT, MAX_STUDENT, are these in test.h? what are they?
        void rmNl ( char s[] );function declarations should be outside of main
        void *fin, *fout;void? should be FILE
        void grades_in(FILE *fin, FILE *fout, STUDENT stuArr[MAX_STUDENTS]);function declaration again
        
       /* Open file to read input data */
       if ( ( fin = fopen ( "grades.in", "r" ) ) == NULL ) {grades.in?
          fprintf ( stderr, "Could not open file grades.in\n" );
          exit ( -1 );
       }
    
       /* Open file for writing output */
       if ( ( fout = fopen ( "grades.out", "w" ) ) == NULL ) {grades.out?
          fprintf ( stderr, "Could not open file grades.out\n" );
          fclose ( fin );
          exit ( -1 );
       }
       grades_in(fin, fout, stuArr);
       system ("PAUSE");    
       return 0;
    }
    give that stuff a try then post again.
    Hope that helps,
    Crazed

  3. #3
    Registered User
    Join Date
    Oct 2005
    Posts
    14
    Thank you for your help. Now I have a question: If I have an input data which looks like this:
    Code:
    John Doe
    1111 
    98 87 69 97 89
    85 91 88
    Bill Doe
    2222
    77 78 87 79 85
    69 73 89
    how do I use fgets and sscanf with it? It seems my code doesn't scan to all the data...

  4. #4
    Registered User
    Join Date
    Oct 2005
    Posts
    14
    This is the latest of my code editted. I just paste the fgets and sscanf part so it will be easier for you guys to examine it:

    Code:
       while( fgets ( buf, MAX_STUDENTS, datain ) != NULL ) {
          rmNl ( buf );
          if( !lineWithBlanks ( buf) ) {
    
                if ( counter == 1 && (buf[0] != ' ')) {
                     if ( sscanf(buf, "%s %s", fname, lname) == 2 ) printf ("%s %s\n", fname, lname);
                     else printf("fail");
                }
                else if ( counter == 2 && (buf[0] != ' ')) {
                     if ( sscanf(buf, "%d", &id) == 1 ) printf ("%d\n", id); 
                     else printf("fail");
                }
                else if ( counter == 3 && (buf[0] != ' ')) {
                     if ( sscanf(buf, "%d %d %d %d %d", &h1, &h2, &h3, &h4, &h5) == 5 ) printf ("%d %d %d %d %d\n", h1, h2, h3, h4, h5);   
                     else printf("fail");
                }
                else if ( counter == 4 && (buf[0] != ' ')) {
                     if ( sscanf(buf, "%d %d %d", &e1, &e2, &e3) == 3 ) printf ("%d %d %d\n", e1, e2, e3);
                     else printf("fail");             
                }
                counter++;
                continue;
    
          }

  5. #5
    Registered User
    Join Date
    Oct 2005
    Posts
    14
    The output of the code is this:
    John Doe
    1111
    98 87 69 97 89
    85 91 88

    It doesn't loop the second time, weird.

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Get rid of the newline left hanging in the buffer. The second call to fgets() will get a blank string, which isn't what you want. FAQ: http://faq.cprogramming.com/cgi-bin/...&id=1043284392
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Extended ASCII Characters in an RTF Control
    By JustMax in forum C Programming
    Replies: 18
    Last Post: 04-03-2009, 08:20 PM
  2. Enforcing Machine Code Restrictions?
    By SMurf in forum Tech Board
    Replies: 21
    Last Post: 03-30-2009, 07:34 AM
  3. Obfuscated Code Contest
    By Stack Overflow in forum Contests Board
    Replies: 51
    Last Post: 01-21-2005, 03:17 PM
  4. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM
  5. Replies: 0
    Last Post: 02-21-2002, 05:05 PM

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