Thread: Re: [Dev-C++] random line from file
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: <wa...@wa...> - 2004-02-13 16:45:27
|
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 |
|
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 > |
|
From: <wa...@wa...> - 2004-02-13 17:45:02
|
On 13 Feb 2004 at 18:10, Per Westermark wrote: > Oooh. Lots of code, and memory intensive if the file is really big. Well, everything's a tradeoff. If you get intensive with the memory, you get the benefit of the code being a little faster. With the algorithm I wrote up, you only have to read the file once and then you can access the line you want immediately. With your first algorithm, you've got to read the same information over and over. And I don't think my version would be bigger code-wise. Your second algorithm is making the assumption that the random number is generated by the program and can refer to a random place in the file as opposed to a random line in the file. I was making the assumption that the random number would be supplied by the user. After re-reading the OP's question, I think you're right. > No need to fight with Windows API unless speed is > absolutely imperative, i.e. normal c/c++ file operations are > quire ok to use. I didn't know that Windows had separate file operations per se. I was assuming he'd use the regular file streams. But I'm not sure if the point of the assignment is to teach them how to read text files or use Windows. If you'll notice, the OP said specifically that the program was to use the Windows API and that refreshes should be done by hitting a button. That looks like the point is more how to write Windows code than to read text files. Watson (the pencil neck) Davis |
|
From: Per W. <pw...@ia...> - 2004-02-13 18:39:29
|
My second algorithm have the problem that it varies the probability of selecting a line with the length of the line. However, it has the advantage that it can process any size of file in constant time with constant memory consumption. Sometimes this is an important trade-off. /Per W On Fri, 13 Feb 2004 wa...@wa... wrote: > On 13 Feb 2004 at 18:10, Per Westermark wrote: > > > Oooh. Lots of code, and memory intensive if the file is really big. > > Well, everything's a tradeoff. If you get intensive with the memory, > you get the benefit of the code being a little faster. With the > algorithm I wrote up, you only have to read the file once and then > you can access the line you want immediately. With your first > algorithm, you've got to read the same information over and over. > > And I don't think my version would be bigger code-wise. > > Your second algorithm is making the assumption that the random number > is generated by the program and can refer to a random place in the > file as opposed to a random line in the file. I was making the > assumption that the random number would be supplied by the user. > After re-reading the OP's question, I think you're right. > > > No need to fight with Windows API unless speed is > > absolutely imperative, i.e. normal c/c++ file operations are > > quire ok to use. > > I didn't know that Windows had separate file operations per se. I > was assuming he'd use the regular file streams. > > But I'm not sure if the point of the assignment is to teach them how > to read text files or use Windows. If you'll notice, the OP said > specifically that the program was to use the Windows API and that > refreshes should be done by hitting a button. That looks like the > point is more how to write Windows code than to read text files. > > 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 > |