[Courses] [C] alright, an exercise 3 answer :)

Morgon Kanter admin at surgo.net
Fri Dec 6 23:45:17 EST 2002


Tested, works, thanks to everybody's help solving my newbie problems :)

Exercise 3 description:
Write a program that reads a file containing a list of numbers and
write two files, one with all numbers divisible by three and the
other containing all the other numbers.

/********* BEGIN EXERCISE 3 *********/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>

void isnull(FILE *file, char *filename);
void writefile(char *buf, FILE *file);

int main(int argc, char *argv[]) {
    FILE *in_file, *threes_file, *others_file;
    int current_char; 
    char current_number[100] = "";
    char temp[2];
    
    /* Check for the right number of arguments */
    if(argc != 4) {
        fprintf(stderr, "Error: Incorrect number of arguments.\n");
        fprintf(stderr, "Usage: exc3 infile threesfile othersfile\n");
        exit(8);
    }

    in_file = fopen(argv[1], "r");
    if(errno == ENOENT) {
        fprintf(stderr, "Error: No input file.\n");
        exit(8);
    }

    threes_file = fopen(argv[2], "w");
    others_file = fopen(argv[3], "w");
    isnull(threes_file, argv[2]);
    isnull(others_file, argv[3]);
    
    current_char = fgetc(in_file);
    while(current_char != EOF) {
        
        /* Make sure this isn't just leading whitespace */
        if(current_char == ' ' && atoi(current_number) >= 0) {
            
            /* Ok, here if the current number modulus 3 is 0, 
             * it is divisible by three, so we write it into 
             * the file where all the numbers divisible by 
             * three go. If it isn't, we'll write it to the 
             * file that all numbers not divisible by three 
             * go. Don't worry about clearing the current_number 
             * string after that, the writefile function will 
             * do it for us.
            */
            if(atoi(current_number) % 3 == 0) {
                writefile(current_number, threes_file);
            } else {
                writefile(current_number, others_file);
            }
        /* Make sure the character is actually a letter instead 
         * of just random garbage inserted by the person.
        */
        } else if(current_char >= '0' && current_char <= '9') {
            current_char -= '0';
            sprintf(temp, "%i", current_char);
            strcat(current_number, temp);
        }
        /* And go to the next. So you see, if the last 
         * character was random garbage, it's just skipped.
        */
        current_char = fgetc(in_file);
    }
    
    /* If there is still a number left in current_number 
     * after we hit EOF, make sure we write it to the 
     * proper file. These are same as above.
    */
    if(atoi(current_number) > 0) {
       if(atoi(current_number) % 3 == 0) {
           writefile(current_number, threes_file);
       } else {
           writefile(current_number, others_file);
       }
    }

    fclose(threes_file);
    threes_file = NULL;
    fclose(others_file);
    others_file = NULL;
    
    printf("Split %s into %s and %s.\n", argv[1], argv[2], argv[3]);

    return 0;
}

void isnull(FILE *file, char *filename) {
   if(file == NULL) {
       fprintf(stderr, "Error: Can't write to %s\n", filename);
       exit(8);
   }
}

void writefile(char *buf, FILE *file) {
    fputs(buf, file);
    fputc(' ', file);

    strcpy(buf, "");
}
/********* END EXERCISE 3 **********/

Wow :p

Morgon
--
grab my updated (11/25/02) public key from http://www.surgo.net/pubkey.asc




More information about the Courses mailing list