newbie help: Input Output Text file question

Discussion in 'C++' started by ComicCaper, May 9, 2006.

  1. ComicCaper

    ComicCaper Guest

    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
     
    ComicCaper, May 9, 2006
    #1
    1. Advertising

  2. 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/
     
    Peter Jansson, May 9, 2006
    #2
    1. Advertising

  3. ComicCaper

    ComicCaper Guest

    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/
     
    ComicCaper, May 10, 2006
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. lallous

    input/output to text file

    lallous, May 6, 2004, in forum: C++
    Replies:
    3
    Views:
    461
    Michiel Salters
    May 7, 2004
  2. Flomo Togba Kwele
    Replies:
    2
    Views:
    363
    Flomo Togba Kwele
    May 2, 2007
  3. Rachel Garrett
    Replies:
    4
    Views:
    378
    Bruno Desthuilliers
    Dec 22, 2007
  4. drew
    Replies:
    6
    Views:
    176
  5. Mav
    Replies:
    22
    Views:
    367
Loading...

Share This Page