Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

A 'Quote of the Day' type program?

0 views
Skip to first unread message

Vance Palodichuk

unread,
Jun 13, 1996, 3:00:00 AM6/13/96
to

To test the C++ I/O functions I decided to write a program that randomly displayed
a 'quote' from a file. I thought this sounded pretty easy. Boy was I wrong! I've
tried and tried--and dammit, I've came SO close--but I never got it just right. So
I think there may be some I/O classes I'm not aware of. So I'd like to see someone's
finished random quoter (or a program similar to it). I'd be very greatful!

Vance
Otaku/2

--
Vance Palodichuk [Team OS/2][Final Fantasy][Anime]
WWW: http://www.usinternet.com/users/vpalodich/
email: vpal...@usinternet.com


Jason Alan Turnage

unread,
Jun 13, 1996, 3:00:00 AM6/13/96
to

Vance Palodichuk (vpal...@usinternet.com) wrote:
: To test the C++ I/O functions I decided to write a program that randomly

: displayed
: a 'quote' from a file. I thought this sounded pretty easy. Boy was I
: wrong! I've
: tried and tried--and dammit, I've came SO close--but I never got it just
: right. So
: I think there may be some I/O classes I'm not aware of. So I'd like to see
: someone's
: finished random quoter (or a program similar to it). I'd be very greatful!

MSVC 4.0 comes with a Tip of the Day feature in the component
gallery. Check it out. I've never used it, but I don't believe
it calls up the tips randomly, though, which I believe you wanted
to do.


--
Jason Turnage
Georgia Tech
tur...@cc.gatech.edu
www.prism.gatech.edu/~gt8678a

Steve Barta

unread,
Jun 14, 1996, 3:00:00 AM6/14/96
to

In article <31bfd...@news.usinternet.com>, vpal...@usinternet.com (Vance Palodichuk) wrote:
>To test the C++ I/O functions I decided to write a program that randomly
> displayed
>a 'quote' from a file. I thought this sounded pretty easy. Boy was I wrong!
>
Vance,
to quote a famous random quote "Everything takes longer than it takes."

Some suggestions (having done this in C).
Approach 1) File contains random-length records.
Create the records with a recognized end-of-record flag (I used \n).
Read the end-of-file position and calc a random number between 0 and that
number.
Drop into the file there and start counting backwards looking for the last
end of record. Then read forward to the next on and display what you got.


Approach 2) File contains fixed-length records.
Easy as pie. Measure the size of the file. Divide by the length of a
record, and you got the number of records. Calc a random number
<= number of records and then drop into the file there and read forward
rec-length bytes.

I've done both approaches and each works great.

Steve Barta
Sunquest Information Systems
Tucson AZ, USA
j...@maurice.sunquest.com

Amelia A Lewis

unread,
Jun 18, 1996, 3:00:00 AM6/18/96
to

As unicorns gamboled in a quiet glade far away, Steve Barta <j...@alpha.sunquest.com>,
in article <4prve9$f...@odin.sunquest.com>, was overheard to say:

>In article <31bfd...@news.usinternet.com>, vpal...@usinternet.com (Vance Palodichuk) wrote:
>>To test the C++ I/O functions I decided to write a program that randomly
>> displayed
>>a 'quote' from a file. I thought this sounded pretty easy. Boy was I wrong!
>>
>Vance,
> to quote a famous random quote "Everything takes longer than it takes."
>
>Some suggestions (having done this in C).
>Approach 1) File contains random-length records.
> Create the records with a recognized end-of-record flag (I used \n).
> Read the end-of-file position and calc a random number between 0 and that
>number.
> Drop into the file there and start counting backwards looking for the last
> end of record. Then read forward to the next on and display what you got.

[fixed-length deleted; I don't think fixed-length is really appropriate for
this problem]

This sounds an awful lot like an old unix standard, fortune. Not to belabor
the issue, fortune uses a different solution (the solution, in fact, is
rather flawed in the available BSD 4.3 and 4.4 derivatives that are
available on the net -- that is, they contain bugs, of severity depending on
what you really want to do). To wit: data files and associated index files.
In fact, the original implementation of fortune used a single file. So let
me describe both of these implementations.

The current implementation is fairly straightforward. Each file of quotes
(fortunes, random text strings) is 80x25 tty ready, marked by a record
delimiter (current versions use 'delimiter' rather than 'terminator,' which
happens to be one of the bugs, since the term seems to have been ambiguous
to the implementors). A separate utility begins by writing a header for the
index file, which contains some status information and the record delimiter
character (it can be varied, though it usually isn't). It then writes the
first pointer to a quote/fortune, at offset 0. It then seeks the next
delimiter, and writes a pointer to the immediately following position. As
enhancements, the pointers can be sorted or randomized (but this is broken
in the BSD releases, due to a "while (i--) {}" loop where i is initialized
to zero, not the the number of pointers in the file).

The previous implementation wrote the header and text strings as part of one
file, which is also relatively straightforward, once the number of pointers
is known (so write the header file, increment all pointers by the file size,
then cat the two together).

The great advantage to this is that the number of fortunes/quotes is stored
in the header, where it is easy to extract, and the random search can be on
fixed-length records (i.e., pointers) in the header or header file, then
leaping quickly to the text string pointed at. The disadvantage, of course,
is the possibility of text and pointers getting out of synch.

All of this, though, is straight C hacking. I'm not at all sure how one
would implement a similar sort of object-oriented program (the natural
objects are the fortunes/quotes, clearly, but the behavior is so strictly
limited that there seems to be little gain here, and presumably for the
objects to be persistent, they would need to be stored in a similar
pointer/string fashion).

Sorry. The unix fortune program is one of my pet fascinations.

Amy!

Winfried Dobbe

unread,
Jun 18, 1996, 3:00:00 AM6/18/96
to

Steve Barta wrote:
>
> In article <31bfd...@news.usinternet.com>, vpal...@usinternet.com (Vance Palodichuk) wrote:
> >To test the C++ I/O functions I decided to write a program that randomly
> > displayed
> >a 'quote' from a file. I thought this sounded pretty easy. Boy was I wrong!
> >

I wrote such a 'quote of the day' program for Windows 3.1 and '95. If you're interested
in the source, mail me.

Winfried Dobbe
mai...@dmu.ac.uk

0 new messages