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 12:00 ID:nIlMuqvl

inline int gameLoop()
{
 input userInput;
 keyState& theKeyState = userInput.theKeyState;

 /* Time values for frame rate throttling. */
 int prevTicks = 0, curTicks = 0;
 int ticksDifference = 0;
 int framesPerSecond = 30; /* desired frame rate (fps) */
 int updateTime = (1000 / framesPerSecond); /* update frame after this many milli-seconds */

    while (theKeyState.quit == false)
    {
     curTicks = SDL_GetTicks();
     ticksDifference = curTicks - prevTicks;
     userInput.updateState(); /* Grab user input */

        if (ticksDifference >= updateTime)
        {
         // Update subsystem and graphics subsystem goes here!

         prevTicks = curTicks; // reset the time difference
         theKeyState.manualReset(); // reset keys that must be manually reset
        }
    }
 return 0;
}

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