LinuxQuestions.org
Visit the LQ Articles and Editorials section
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 10-29-2005, 08:14 PM   #1
twirl
Member
 
Registered: Aug 2005
Posts: 132

Rep: Reputation: 15
Reading random lines in c++?


Hi,

I have tried searching google on how i can do this, but i have found no tutorials on it anywhere.

What i would like to be able todo is :-

1. Open a file and add numbers to each line in that file.
2. Randomize the lines
3. Output a random line from the file for user output.
4. Put it all into a loop so it randomizes all the time.

If anyone could show me a small snippet it would be very much appreciated

Thankyou

twirl
 
Old 10-29-2005, 08:46 PM   #2
dmail
Member
 
Registered: Oct 2005
Posts: 970

Rep: Reputation: Disabled
doesnt this operation seem a better way of doing that?

open file
store the lines in a data structure
close file

while(u_have_not_finished)
{
randomise the data structure or generate a random index into the structure
output a line to console
}


or do you need to do it your way?

Last edited by dmail; 10-29-2005 at 08:51 PM.
 
Old 10-29-2005, 11:04 PM   #3
twirl
Member
 
Registered: Aug 2005
Posts: 132

Original Poster
Rep: Reputation: 15
Hi,

Your way looks better, just not sure how. thankyou

twirl
 
Old 10-30-2005, 03:19 AM   #4
naf
Member
 
Registered: Oct 2005
Location: Chicago, USA
Distribution: Slackware & Fedora
Posts: 66

Rep: Reputation: 15
twirl, you need to be more specific:
1. What is the format on the output?
2. Are the contents to be written out to the file again (overwriting the file or to a new file)?
3. Is there supposed to be some way out of the infinite loop from your step 4?

I feel that I am misunderstanding your requirements, but this is what I have:

Code:
#include <iostream>
#include <vector>
#include <fstream>
#include <ctime>
#include <string>
#include <iomanip>

int random_line( const char *filename );

int main( int argc, char **argv )
{
    if( argc != 2 )
        std::cout << "Usage: " << argv[0] << " <filename>" << std::endl;
    else
        random_line( argv[1] );
    
    return 0;
}

int random_line( const char *filename )
{
    // Produce a random line continuously.
    std::ifstream fptr( filename, std::ios::in );
    std::vector< std::string > positions;
    std::vector< std::string >::size_type line;
    std::string buffer;
    
    // Read the contents in.
    while( getline( fptr, buffer, '\n' ) )
        positions.push_back( buffer );
    
    // Explicit close.
    fptr.close();
    
    // Forever print a random line from the source file in the format of
    // <line number> = <line>
    for( srand( unsigned( time( 0 ) ) ); ; )
        line = static_cast< std::vector< std::string >::size_type >( ( double( rand() ) / RAND_MAX ) * positions.size() ),
        std::cout << std::setw( 12 ) << line << " = " << positions[ line ].c_str() << std::endl;

    // Never reaches here.
    return 0;
}
And running with itself as an argument.
Code:
tcsh_prompt>g++ random_line.cpp && a.out random_line.cpp | head -20 
          38 =         std::cout << std::setw( 12 ) << line << " = " << positions[ line ].c_str() << std::endl;
          39 = 
          36 =     for( srand( unsigned( time( 0 ) ) ); ; )
          17 = }
          17 = }
           8 = 
          11 =     if( argc != 2 )
          30 =     
          24 =     std::vector< std::string >::size_type line;
          30 =     
           1 = #include <vector>
          15 =     
          28 =     while( getline( fptr, buffer, '\n' ) )
          31 =     // Explicit close.
           4 = #include <string>
           3 = #include <ctime>
          29 =         positions.push_back( buffer );
          21 =     // Produce a random line continuously.
           9 = int main( int argc, char **argv )
           3 = #include <ctime>
 
Old 10-30-2005, 03:58 AM   #5
twirl
Member
 
Registered: Aug 2005
Posts: 132

Original Poster
Rep: Reputation: 15
Hi,

Thankyou for you're reply.

I meant what i want to do is :-

1. Open a txt file
2. add numbers to each line in that txt file
3. save that txt file with the numbers added without deleting what is already in the txt file.
4. randomize the lines


I have though about making the text file myself and putting the numbers in myself, but i would prefer to
use the above method.

Then i am going add some input/output so it will ask the user to type in a question then it will give them a
random answer from the txt file. I also want to be able to add a feature so the user can add there own quotes
and save it.

Thankyou

twirl
 
Old 10-30-2005, 09:50 AM   #6
naf
Member
 
Registered: Oct 2005
Location: Chicago, USA
Distribution: Slackware & Fedora
Posts: 66

Rep: Reputation: 15
Ok, so I still have questions:
How do you do step 3 by saving the changes to the text file without modifying the text file? Either you are overwriting or saving to a different file. Which is it?
Why modify the original text file with prefixed line numbers and randomized positions? If you re-run this operation then the contents will have two numbers associated with each line.
Why not just count the number of lines saving the indexes like dmail suggested earlier and randomly select that single line? Is it necessary that the original file be modified? If so, then your users may introduce errors.
 
Old 10-30-2005, 11:03 AM   #7
twirl
Member
 
Registered: Aug 2005
Posts: 132

Original Poster
Rep: Reputation: 15
Hi,

Sorry its my bad i read it wrong, i meant for it to output to a new txt file .
No its not important the users can modify it.

Thankyou

twirl
 
Old 10-30-2005, 11:11 AM   #8
twirl
Member
 
Registered: Aug 2005
Posts: 132

Original Poster
Rep: Reputation: 15
Hi,

I also forgot to mention the loop for the random lines has to be infinite so it always gets randomized.

Thankyou

twirl
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Reading the number of Lines in a File Mistro116@yahoo.com Programming 31 11-24-2005 01:36 AM
printer printing vertical lines at beginning and end of lines makhand Linux - Hardware 0 09-02-2005 03:03 PM
Creating random numbers from shell with /dev/random khermans Linux - General 1 07-13-2004 01:12 PM
Screen goes blank(random) to vertical lines bruce1271 Linux - General 2 11-15-2003 12:02 PM
How many lines? WindozBytes Linux - General 17 03-30-2002 08:10 PM


All times are GMT -5. The time now is 02:20 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration