home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
The Fred Fish Collection 1.5
/
ffcollection-1-5-1992-11.iso
/
ff_disks
/
300-399
/
ff384.lzh
/
NorthC
/
Example2.LZH
/
fortune
/
fortune.c
< prev
next >
Wrap
C/C++ Source or Header
|
1990-08-30
|
6KB
|
212 lines
/* (c) 1990 S.Hawtin.
Permission is granted to copy this file provided
1) It is not used for commercial gain
2) This notice is included in all copies
3) Altered copies are marked as such
No liability is accepted for the contents of the file.
*/
/**********************************************************************/
/*
This is a short example program that selects a phrase from a file and
prints it.
*/
/* We are going to print stuff to files so we must have "stdio.h" */
#include <stdio.h>
/**********************************************************************/
/* First a set of constants */
/*
The data file that all the phrases come from.
This file must be at least MIN_LENGTH charcters long, and have
more than 3 phrases in.
*/
#define MIN_LENGTH 100
#define SRC_FILE "fortune.txt"
/*
Some special "characters", to indicate conditions other than
normal characters read in from the file.
*/
#define MY_EOF -1
#define MY_EOL -2
/**********************************************************************/
/* Some special NorthC things */
#ifdef NORTHC
extern long _fromWB; /* Set to 0 if called from the CLI */
/*
The description of a console window to use if called from the
workbench
*/
char *_WBConsole="con:20/40/600/70/NorthC: Fortune Program";
#endif
/**********************************************************************/
/* The functions */
static int
tyi(fptr)
FILE *fptr;
{/*
Input a single character from the file, if we find the end of
a phrase return MY_EOL, if we find the end of a file return MY_EOF.
*/
/* Somewhere to keep the characters as they are read */
int next;
/* Just get the next character from the file */
next = fgetc(fptr);
/*
Now we have three special cases,
1) the '\' charcter: Read the next character without
translating it. This lets us embed newlines in a phrase.
2) the newline charcter: Tell the caller we have got the
end of the phrase.
3) the end of file: Tell the caller we have an end of file.
*/
if(next=='\\')
{next = fgetc(fptr);
if(next=='n')
next = '\n';
}
else if(next=='\n')
next = MY_EOL;
else if (next==EOF || feof(fptr))
next = MY_EOF;
return(next);
}
static int
find_next(fptr)
FILE *fptr;
{/*
Keep reading until we find the end of the current phrase, when
we have the end of phrase return true if we did not get the end
of file as well.
*/
int next;
next = tyi(fptr);
while(next!=MY_EOL && next!=MY_EOF)
next = tyi(fptr);
return(next!=MY_EOF);
}
static int
out_next(fptr)
FILE *fptr;
{/*
Output the next phrase to the terminal, this involves reading the
phrase and outputting until we run out of characters.
Again if we found the end of the file return false.
*/
int next;
next = tyi(fptr);
while(next!=MY_EOL && next!=MY_EOF)
{fputc(next,stdout);
/* Put four spaces at the start of each line */
if(next=='\n')
puts(" ");
/* Get the next charcter from the file */
next = tyi(fptr);
}
return(next!=MY_EOF);
}
void
main(argc,argv)
int argc; /* Both arguments are unused in this example */
char **argv;
{/* Pick a single saying from a file and send it to the user */
FILE *fptr; /* The data file */
long length; /* The total length of the data file */
long where; /* Where we are going to look for our phrase */
/* First open the data file */
fptr = fopen(SRC_FILE,"r");
/* Check to make sure that we got it */
if(fptr==NULL)
{printf("Remember:\n\n A good user always keeps\n");
printf(" \"%s\" with his fortune program\n",SRC_FILE);
exit(20);
}
/*
Seed the random number generator, use the current time as a
good source of random numbers
*/
srand(time(NULL));
/* Find out how long the file is, first move to the end */
fseek(fptr,0L,SEEK_END);
/* Then ask where we now are */
length = ftell(fptr);
/*
Check that the file is long enough, this is not really
required but we might get problems with small files
*/
if(length<MIN_LENGTH)
{printf("Remember:\n\n A good user makes sure \"%s\"\n",SRC_FILE);
printf(" is longer than %d charcters.\n",MIN_LENGTH);
exit(20);
}
/* Output a wrapper to put round the phrase */
printf("Remember:\n\n ");
/* Now go look for a phrase in the file */
do {
/* Select somewhere in the middle of the file */
where = rand() % length;
/* Move there */
fseek(fptr,where,SEEK_SET);
/*
Now move to the end of the prase we were in the middle of,
then output the next phrase. If either function returns
false then we hit the end of the data file and must go round
and try again another place in the file.
*/
} while (!find_next(fptr) || !out_next(fptr));
/* Round the phrase off with a couple of newlines */
puts("\n\n");
#ifdef NORTHC
if(_fromWB)
{/* Wait for the user to hit a key before quitting the program */
printf("Hit <Return>:");
/* OK make sure that the last bit gets sent for buffered stdout */
fflush(stdout);
fgetc(stdin);
}
#endif
/* Close the data file */
fclose(fptr);
}