13 Feb, 2007, gazzy123 wrote in the 1st comment:
Votes: 0
I'm using this code below and in the file I got lines.
example

"this is a quotestring\n\randthis newline"
"…"

etc


but it's not doing a linebreak at the \n\r and I cannot figure out so I'm hoping someone else can help me.

void s_all2(char *txt)
{
DESCRIPTOR_DATA *d;
FILE *fp;
char buf[MAX_STRING_LENGTH];
char buf2[MAX_STRING_LENGTH];
int number;
int lines=0;
int i;

BN(buf);
BN(buf2);

if ((fp = fopen(HINT_FILE, "r")) != NULL)
{
lines = 0;
while(!feof(fp))
{
fgets(buf2, MAX_STRING_LENGTH, fp);
lines++;
}
fclose(fp);
}
if (lines <= 0) lines = 0;
srand(time(0));
number = (1 + (rand() % lines));

if ((fp = fopen(HINT_FILE, "r")) != NULL)
{
for(i=1;!feof(fp); i++)
{
fgets(buf2, MAX_STRING_LENGTH, fp);
if (i == number)
{
fgets(buf2, MAX_STRING_LENGTH, fp);
break;
}
}
fclose(fp);
}
else
{
fp = fopen(HINT_FILE, "w");
fputs("hints\n", fp);
fclose(fp);
}

if (buf2 == '\0')
snprintf(buf,sizeof(buf),"\n\r{R*{YQuote of the Tick{R*{C %s{x\n\r", "error");
else
{
snprintf(buf,sizeof(buf),"\n\r{R*{YQuote of the Tick{R*{C %s{x\n", buf2);
}

for (d = descriptor_list; d ; d = d->next)
{
if (d->connected == CON_PLAYING)
{
if (!IS_SET(d->character->comm,COMM_NOHINT))
send_to_char(buf,d->character);
}
}
return;
}
13 Feb, 2007, Zeno wrote in the 2nd comment:
Votes: 0
Perhaps \n is being parsed to \\n beforehand or the sort?
13 Feb, 2007, Omega wrote in the 3rd comment:
Votes: 0
why not use fread_to_eol to get the end-line.

and second off, why don't you just load that data into a linked list, instead of parsing the random quote by loading/unloading the file every tick. it would save time, and make your random quote of the tick work allot easier.
14 Feb, 2007, gazzy123 wrote in the 4th comment:
Votes: 0
if the line read from the file is:
"this is a quote\n\rand this is still the same quote with newline"

then it sends out something like

*quote* "this is a quote\n\rand this is still the same quote with newline"

but it doesn't use \n\r it should be a newline. anyone got any ideas. I posted the functions below

void send_to_all(char *chan,char *txt,int invis, int incog, long flag)
{
DESCRIPTOR_DATA *d;
char buf[MSL];

BN(buf);

if (IS_NULLSTR(txt))
return;

if (IS_NULLSTR(chan))
snprintf(buf,sizeof(buf),"\n\r%s{x\n\r",txt);
else
snprintf(buf,sizeof(buf),"\n\r%s %s{x\n\r",chan,txt);

for (d = descriptor_list; d ; d = d->next)
{
if (incog > 0)
if (d->character->level < incog)
continue;
if (invis > 0)
if (d->character->level < invis)
continue;

if (d->connected == CON_PLAYING)
{
if (flag > 0)
if (IS_SET(d->character->comm,flag))
continue;

send_to_char(buf,d->character);
}
}
return;
}
void load_quotes(void)
{
FILE *fp;
QUOTE_DATA *pQuote;
char buf[MSL];
int number;

fp = fopen(QUOTE_FILE,"r");
for(number=0;!feof(fp); number++)
{
fgets(buf, MAX_STRING_LENGTH, fp);
CREATE(pQuote,QUOTE_DATA,1);
pQuote->number = number;
pQuote->text = str_dup(buf);

top_quotes++;

pQuote->next = quote_list;
quote_list = pQuote;
}
fclose(fp);
return;
}
void random_quote(void)
{
QUOTE_DATA *quote,*quote_next;
int random=0;

random = (1 + (rand() % top_quotes));
for (quote = quote_list; quote; quote = quote_next)
{
quote_next = quote->next;
if (quote->number == random)
{
send_to_all("{R*{YQuote of the Tick{R*{C",quote->text,0,0,COMM_NOHINT);
break;
}
}
}
14 Feb, 2007, Davion wrote in the 5th comment:
Votes: 0
Why aren't you using ROM? ROM comes with a bunch of nice file reading, and random number generating functions. Why don't you use fread_string() to grab the quotes? And have you actually tested doing
random = (1 + ( rand() % top_quotes) );

The random number will be the same every time.

Observe
aaron@beast:~/code/tests> cat test.cpp
#include <iostream>

int main()
{ int random;
random = (1+ (rand(time(0)) % 20 ) );

std::cout << random << std::endl;
return -1;
}

aaron@beast:~/code/tests> ./test
4
aaron@beast:~/code/tests> ./test
4
aaron@beast:~/code/tests> ./test
4
aaron@beast:~/code/tests> ./test
4
aaron@beast:~/code/tests> ./test
4
aaron@beast:~/code/tests> ./test
4


Use number_range(1, top_quotes); you'll get much more, uhh, inconsistent results ;).
15 Feb, 2007, Tyche wrote in the 6th comment:
Votes: 0
Davion said:
And have you actually tested doing


Observe:
$ cat rnd.c
int main() {
#define top_quotes 50
int i;
for (i=0;i<10;i++) {
int random = (1 + ( rand() % top_quotes) );
printf("%d\n", random);
}
}

$ gcc rnd.c ; ./a
1
34
44
13
30
1
9
3
7
7
15 Feb, 2007, Davion wrote in the 7th comment:
Votes: 0
If you run it again, you'll get the exact same results. That's like… semirandom ;). I say use number_range()!
15 Feb, 2007, Tyche wrote in the 8th comment:
Votes: 0
Davion said:
If you run it again, you'll get the exact same results. That's like… semirandom ;). I say use number_range()!


Given the same test program, number_range() will produce repeatable results on every run as well.
You aren't seeding the random number generator in your test program. It's my assumption that since he's running a Merc/Rom/Smaug something or other that the random number generator is seeded in boot_db.

Usually with… srand(time) or srand(time*pid) or something.
15 Feb, 2007, gazzy123 wrote in the 9th comment:
Votes: 0
the random works just fine and fread_string messes up the reading so I had to use fgets
but thats true I should feed the generator something before actually using the rand added a srand(time(0)) before tha random = …..

its the \n\r problem I have. I guess I write up a function fread_line and do it someway or something
0.0/9