Valhalla Legends Archive

Programming => General Programming => C/C++ Programming => Topic started by: warz on July 17, 2005, 09:17 PM

Title: Reading random line from a file
Post by: warz on July 17, 2005, 09:17 PM
Which method would be best for a random quote idle message: saving quotes file to an array and pulling from the array, or reading a line from the file every time the idle message is sent?

I want the quotes.txt to be editable while the programing is running - so new quotes can be added. So maybe an array wouldn't be the easiest way? Wouldn't a large array, with a bunch of data in it also be memory consuming?

Just wondering what everyone thinks. Also, wasn't there a method of doing this in C++ on botdev.valhallalegeneds.com (wherever that site went)?
Title: Re: Reading random line from a file
Post by: UserLoser. on July 17, 2005, 10:44 PM
Quote from: warz on July 17, 2005, 09:17 PM
Also, wasn't there a method of doing this in C++ on botdev.valhallalegeneds.com (wherever that site went)?

Somewhat (http://botdev.valhallalegends.com/documents/creadquotes.html).
Title: Re: Reading random line from a file
Post by: warz on July 17, 2005, 11:24 PM
This might work a little bit better.


#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <windows.h>

void SendRandomQuote(void);

int main(void) {
while(1){
SendRandomQuote();
Sleep(500);
}

return 0;
}

void SendRandomQuote(void) {
char *test[200];
    char buf[255];
    int i = 0, x = 0, y = 0;

    FILE *fp = fopen("Quotes.txt", "r");
    if (fp) {
           while (fgets(buf, sizeof(buf), fp)) {
                   test[i] = (char *)malloc(strlen(buf)+1);
                   strcpy(test[i], buf);
                   i++;
           }
    }
    for(x=0; x < i ; x++) {
            //printf("%s", test[x]);
    }
    //x = 0;

y = (GetTickCount() % x);

    printf("%s", test[y]);
}
Title: Re: Reading random line from a file
Post by: Kp on July 18, 2005, 09:07 PM
Beware that warz's method will induce stack corruption if you have more than 200 quotes.
Title: Re: Reading random line from a file
Post by: warz on July 18, 2005, 09:20 PM
Yeah, but if you've got more than 200 quotes also beware that you need to reduce your amount of quotes. ;-)
Title: Re: Reading random line from a file
Post by: Mephisto on July 18, 2005, 09:52 PM
Quote from: warz on July 18, 2005, 09:20 PM
Yeah, but if you've got more than 200 quotes also beware that you need to reduce your amount of quotes. ;-)

That's stupid.  You should make your functions and designs generic whenever possible and above that non-error-prone which is blatantly obvious in your function.
Title: Re: Reading random line from a file
Post by: warz on July 19, 2005, 06:42 AM
Quote from: Mephisto on July 18, 2005, 09:52 PM
Quote from: warz on July 18, 2005, 09:20 PM
Yeah, but if you've got more than 200 quotes also beware that you need to reduce your amount of quotes. ;-)

That's stupid.  You should make your functions and designs generic whenever possible and above that non-error-prone which is blatantly obvious in your function.

Uh, yeah, that's the idea.