Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

ITT we discuss this code

Name: Anonymous 2007-03-19 9:48 ID:T6Q9tY7E

1:

int main(int argc, char **argv) {
    FILE *fp=fopen(argv[1], "r");
    while(feof(fp)) putchar(getc(fp));
    return fcloseall();
}


2:

int main(int argc, char **argv) {
    FILE *fp; // declaring a file pointer
    char c = 0; // allocating 1 byte to store the byte read from file
   
    if(argc != 2) { // checking for arguments
        fprintf(stderr, "Error: usage: %s [file] \r\n", argv[0]); // printing error message
        return 1;
    }
    fp = fopen(argv[1], "rb"); // opening argv[1]
    if(fp == NULL) { // checking for errors
        perror("fopen"); // printing error message
        return 2;
    }
    while(c = getc(fp)) { // reading next char from file
        if(c == EOF) { // checking for end-of-file
            fclose(fp); // closing file
            return 0;
        }
        putchar((int)c); // printing byte
    }
    fclose(fp); // closing file
    return 0;
}


3:

int main(int argc, char **argv) {
   
    FILE *fp;
    char *buffer;
    long filesize;
    if(!(fp = fopen(argv[1], "r"))) {
        perror("fopen");
        return fcloseall();
    }
    fseek(fp, 0, SEEK_END);
    filesize = ftell(fp);
    rewind(fp);
    if(!(buffer = (char*)malloc(filesize+1))) {
        perror("malloc");
        return fcloseall();
    }
    if(fread(buffer, sizeof(char), (size_t)filesize, fp) != filesize) {
        perror("fread");
        goto __exit;
    }
    if(fwrite(buffer, sizeof(char), filesize, stdout) != filesize) {
        perror("fwrite");
        goto __exit;
    }
   
    __exit:
    free(buffer);
    fcloseall();
    return 0;
}

What would you prefer

Name: Anonymous 2007-03-19 19:26 ID:zxMSZcdG

>>1
All versions suck due to various reasons.

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List