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

C - read a random line from a file.

Name: Anonymous 2006-03-06 10:31

dear world4chan, I wrote this to return a random line read from a file, but I hate it. what would you do?

#define MAX_LINE_SIZE 1024

void readQuote(int sock_desc)
{
        //data
        FILE* quoteSource;      //source file pointer
        int numLines;           //max number of lines
        int rndLine;            //random line
        int i;
        char buff[MAX_LINE_SIZE] = {0};

        //initialize the random number generator
        srand(time(NULL));

        //open file; check for errors
        quoteSource = fopen("quotes.txt", "r");
        if (quoteSource == NULL)
        {
                printf("!! Error accessing file.\n");
                return;
        }

        //get the file size, if zero, exit
        numLines = 0;
        while (!feof(quoteSource))
                if (fgetc(quoteSource) == '\n') numLines++;
        rewind(quoteSource);

        if (numLines == 0)
        {
                printf("!! Quote file empty\n");
                fclose(quoteSource);
                return;
        }

        //select a line and read until that line is found
        rndLine = (rand() % numLines) + 1;
        printf("line %d of %d: ", rndLine, numLines);

        for (i = 0; i < rndLine; i++)
                fgets(buff, MAX_LINE_SIZE, quoteSource);

        printf("%s\n", buff);


        fclose(quoteSource);
}

Name: Anonymous 2010-09-21 13:47

>>40
God damn it, you're such a fuckface. I hope you choke on a million dicks.

Name: VIPPER 2010-09-21 13:52

I must say, i enjoy this thread.

Name: Anonymous 2010-09-21 14:34

>>41
Why you so mad bro?

Name: sage 2010-09-21 15:26

>>43
Do we really have to point it out?
38  Name: Anonymous : 2006-03-23 18:24  (sage)
>>37 is very experienced, please read his words carefully.
40 Name: Anonymous : 2010-09-21 13:23
I wouldn't want to read a random line from a file in C.

Name: Anonymous 2010-09-21 16:29

>>1
Being from four years ago is not an excuse not to use the [code] tags.

Name: Anonymous 2011-09-23 11:53

[tt]nigger[/tt]

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