|
From: Per W. <pw...@ia...> - 2004-02-13 17:12:46
|
Oooh. Lots of code, and memory intensive if the file is really big. Simplest solution: Read through file - count lines. Generate a random number based on line count. Rewinde file. Read until nth line. Faster solution for large files, but not completely random: Check file size. Generate random number based on file size. Seek to specified position. Scan to next newline and return next line. Since this algorithm may never return the first line, either add special case that random position 0 doesn't need to read past a newline character before returning a value, or add a filler line first. Also - be prepared that too large random number will read past last line. If max line length is known, the second solution may be modified to reading a block of text around the random position. If max line length is 100, load 100 characters before and after random position. Then scan backwards to start of line and return. No need to fight with Windows API unless speed is absolutely imperative, i.e. normal c/c++ file operations are quire ok to use. /Per W On Fri, 13 Feb 2004 wa...@wa... wrote: > On 13 Feb 2004 at 16:26, Can Canova wrote: > > > hi, > > I'm trying to make a windows api program which will take a random line > > from txt file , will be refresh with button . Is there any one who can > > help me? Thanks Can ------------------ > > Here's basically what you want to do: > > 1. You should probably read the entire text file into memory. > 2. Treat each line in the text file as a string. > 3. Load each of these strings into a vector of strings. > 4. Then you can grab a random number from 1 to the size of the > vector to grab a random line. > > That's the basix of what you're going to have to do. > > However, if you're going to have to do this through windows, you're > going to have to learn how to get input and output working with the > API. There are several Windows tutorials out there that can help you > do that. Take a look at: > http://freespace.virgin.net/james.brown7/tutorials.html > http://www.foosyerdoos.fsnet.co.uk/ > > Although there really are a ton of tutorials if you just search for > Windows C++ tutorial. > > Watson (the pencil neck) Davis > > > > ------------------------------------------------------- > SF.Net is sponsored by: Speed Start Your Linux Apps Now. > Build and deploy apps & Web services for Linux with > a free DVD software kit from IBM. Click Now! > http://ads.osdn.com/?ad_id=1356&alloc_id=3438&op=click > _______________________________________________ > Dev-cpp-users mailing list > Dev...@li... > TO UNSUBSCRIBE: http://www23.brinkster.com/noicys/devcpp/ub.htm > https://lists.sourceforge.net/lists/listinfo/dev-cpp-users > |