Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > newbie help: Input Output Text file question

Reply
Thread Tools

newbie help: Input Output Text file question

 
 
ComicCaper
Guest
Posts: n/a
 
      05-09-2006
Hi all,

I'm trying to write a program that will pull 3 random lines from a list
of sentences. I have been doing some looking and found that I can
randomize the character at which the program will pull from in the text
file, but is there a way to simply randomize the line number and so I
can pull the whole line?

Thanks,

Matt
 
Reply With Quote
 
 
 
 
Peter Jansson
Guest
Posts: n/a
 
      05-09-2006
ComicCaper wrote:
> Hi all,
>
> I'm trying to write a program that will pull 3 random lines from a list
> of sentences. I have been doing some looking and found that I can
> randomize the character at which the program will pull from in the text
> file, but is there a way to simply randomize the line number and so I
> can pull the whole line?
>
> Thanks,
>
> Matt


Hi,

Yes. A suggestion follows:

1. Loop through the whole fie to find out the number of lines in it.
2. Generate three random numbers between 1 and the number of lines in
the file.
3. Open the file once more and read the three lines corresponding to the
three random numbers.

Hint: A line in the file may be read using std::getline.


Sincerely,

Peter Jansson
http://www.p-jansson.com/
http://www.jansson.net/
 
Reply With Quote
 
 
 
 
ComicCaper
Guest
Posts: n/a
 
      05-10-2006
Peter,

Thanks for the hint. It's nice to know that there is a simple way to do
this. With my limited knowledge you don't EVEN want to know how I was
thinking I had to do it. (Lots of sorting and comparing, like trying to
get from here to there with right turns only.)

-Matt



Peter Jansson wrote:
> ComicCaper wrote:
>> Hi all,
>>
>> I'm trying to write a program that will pull 3 random lines from a
>> list of sentences. I have been doing some looking and found that I can
>> randomize the character at which the program will pull from in the
>> text file, but is there a way to simply randomize the line number and
>> so I can pull the whole line?
>>
>> Thanks,
>>
>> Matt

>
> Hi,
>
> Yes. A suggestion follows:
>
> 1. Loop through the whole fie to find out the number of lines in it.
> 2. Generate three random numbers between 1 and the number of lines in
> the file.
> 3. Open the file once more and read the three lines corresponding to the
> three random numbers.
>
> Hint: A line in the file may be read using std::getline.
>
>
> Sincerely,
>
> Peter Jansson
> http://www.p-jansson.com/
> http://www.jansson.net/

 
Reply With Quote
 
 
 
Reply

Thread Tools

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Newbie question - what's the term for input/output to a web page? Rachel Garrett Python 4 12-22-2007 09:52 PM
Re: text file search to text file output possible? Whiskers Computer Support 3 10-07-2006 06:32 PM
Re: text file search to text file output possible? Mitch Computer Support 0 10-06-2006 11:15 PM
Newbie: How do I filter output to the screen and writing the orginal output to a file? Mav Perl Misc 22 07-09-2004 09:56 PM
input/output to text file lallous C++ 3 05-07-2004 01:42 PM



Advertisments