Shoutbox

[C help] how to... - Printable Version

-Shoutbox (https://shoutbox.menthix.net)
+-- Forum: MsgHelp Archive (/forumdisplay.php?fid=58)
+--- Forum: Skype & Technology (/forumdisplay.php?fid=9)
+---- Forum: Tech Talk (/forumdisplay.php?fid=17)
+----- Thread: [C help] how to... (/showthread.php?tid=27009)

[C help] how to... by crank on 06-10-2004 at 02:32 PM

I'm working on a new version of QTSender (look over here)

Here's my question:
How do I output a random line from a text file with a command.

code:
This is the content of hello.txt in the folder C:\...\plugins\QTSender\.

\x03#0000FFHello there!
\x03#0000FFHey you!
\x03#0000FFHello! How are you?
\x03#0000FFHey my friend!
\x03#0000FFHello buddy!


I know the formatting will work the only thing I'm wondering about is how to load it.
EXAMPLE:
You type /xQThello

and it outputs:
Hello buddy!

I'm working in C.
RE: [C help] how to... by RaceProUK on 06-11-2004 at 10:09 AM

code:
int <result> = rand() % <out-of>;
The rand() function returns a number from 0 to MAX_INT (32,767 I think). By using the modulo division operator %, you can replace <out-of> with an integer e.g. use 10 to get a random number from 0-9 inclusive.
RE: [C help] how to... by crank on 06-11-2004 at 01:26 PM

quote:
Originally posted by raceprouk
code:
int <result> = rand() % <out-of>;
The rand() function returns a number from 0 to MAX_INT (32,767 I think). By using the modulo division operator %, you can replace <out-of> with an integer e.g. use 10 to get a random number from 0-9 inclusive.

I know that I'm just wondering how to get it from the file...
RE: [C help] how to... by RaceProUK on 06-11-2004 at 01:30 PM

Ah right. In that case, what I would do is read each line of text from the file into an array, then use the random number generated to index the array.
Reading the file in: I would use a while loop, checking for an EOF. Of course, to make the array in the first place, the number of lines in the file would need to be known.
It may be worth seeing if C++ has a class similar to Java's ArrayList, which implements an expandable array of objects.


RE: [C help] how to... by crank on 06-11-2004 at 01:38 PM

I never worked with files before so some extra info would be nice...
I'm not working on the programm till next week tough...

(exams)


RE: [C help] how to... by RaceProUK on 06-11-2004 at 01:41 PM

I've never worked with files in C/C++ either, so I'll refer you to the rather good (if a little hard to use) MSDN Website. Just search for 'file reading C' and you should get some relevant results.

Edit: OK, so that search doesn't help much. Searching for 'fopen' may work better.
Also, make sure you are reading Visual C++ documentation.