Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

random quote generator

Name: Anonymous 2008-09-10 14:35

I was wondering what everyone thought of writing a random quote generator in C++ for XP/Vista.  I'm having a hard time with it.  I wanted it not to have a regular GUI, just have a system tray icon that would pop up a message bubble with the random quote from a .txt file, say every hour or so.  I think I figured out the bubble thing, but getting the program to take a random quote from the text file is driving me crazy.  Do I have to make the quotes in separate strings in the code of the program, so I can get them to pop up every hour in a bubble window?

Name: Anonymous 2008-09-10 16:32

>>4

#define QUOT_DELIM '~'
/* caller frees! */
char *get_quote(char *fname) {
 FILE *infile;
 char *str;
 int i,c;
 int idx = rand()%NUM_LINES;
 int str_used=0,str_size=64;
 if(!(infile=fopen(fname,"rb"))||!(str=malloc(str_size)))
  return 0;
 for(i=0;i<idx;i++) {
  while((c=fgetc(infile))!=QUOT_DELIM && c!=-1)
   ;
  if(c==-1) {
   free(str);
   return 0;
  }
 }
 do {
  if(str_used>=str_size-1) {
   char *new_str;
   if(!(new_str=realloc(str,2*str_size))) {
    free(str);
    fclose(infile);
    return 0;
   }
   str = new_str;
   str_size *=2;
  }
  c = fgetc(infile);
  str[str_used++]=c;
 } while(c!=QUOT_DELIM && c!=-1);
 str[str_used-1]=0;
 fclose(infile);
 return str;
}


Not tested, written in about 2 minutes.

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List