home *** CD-ROM | disk | FTP | other *** search
/ ftp.update.uu.se / ftp.update.uu.se.2014.03.zip / ftp.update.uu.se / pub / rainbow / msdos / misc / tao.zip / TAO.C < prev    next >
C/C++ Source or Header  |  1993-07-01  |  2KB  |  86 lines

  1. /******************************************************/
  2. /** Anonymous ** Tao Te Ching Text Reader ** 7/01/93 **/
  3. /******************************************************/
  4. /**           Works with Turbo C++ v.3.0             **/
  5. /******************************************************/
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <conio.h>
  9. #include <time.h>
  10.  
  11. int main(int argc, char *argv[])
  12. {
  13.     int place, num, count, chapters, random_flag, pause_flag, screen_lines;
  14.     char numtxt[4], outline[80];
  15.     FILE *infile, *inoutfile;
  16.  
  17.     if((infile=fopen("tao.txt", "rt"))==NULL)
  18.     {
  19.         printf("Cannot open tao.txt.\n");
  20.         return 1;
  21.     }
  22.     if((inoutfile=fopen("tao.ini", "rt"))==NULL)
  23.     {
  24.         printf("Cannot open tao.ini.\n");
  25.         fclose(infile);
  26.         return 2;
  27.     }
  28.     if(argc==1) /* no arguments */
  29.         /* get chapters, place, random_flag and pause_flag from ini file */
  30.         fscanf(inoutfile, " %d\n %d\n %d\n %d\n %d", &chapters, &place, &random_flag, &pause_flag, &screen_lines);
  31.     else  /* use cmd line arg and check for error */
  32.     {
  33.         fscanf(inoutfile, " %d\n %d\n %d\n %d\n %d", &chapters, &place, &random_flag, &pause_flag, &screen_lines);
  34.         place=atoi(argv[argc-1]);
  35.         random_flag=0;
  36.     }
  37.     if(random_flag)
  38.     {
  39.         randomize();
  40.         place=(rand()%chapters)+1;
  41.     }
  42.     else if(place<1 || place>chapters) place=1;
  43.     if(screen_lines <=0) screen_lines=25;
  44.     fclose(inoutfile); /* closes the read-only version */
  45.     while(fgets(numtxt, 4, infile), num=atoi(numtxt), num != place)
  46.     {
  47.         if(feof(infile))
  48.         {
  49.             printf("Reached end-of-file in tao.txt.\n");
  50.             printf("You have an invalid tao.ini file.\n");
  51.             fclose(infile);
  52.             return 3;
  53.         }
  54.     }
  55.     clrscr();
  56.     printf("------------%d------------\n", place);
  57.     count=0;
  58.     while(fgets(outline, 80, infile), count++, outline[0]!='*')
  59.     {
  60.         printf("%s", outline);
  61.         if(count==(screen_lines-2)) /* allows for >screen_lines line passages */
  62.         {
  63.             getch();
  64.             printf("------------%d------------\n", place);
  65.         }
  66.     }
  67.     if(pause_flag) getch();
  68.     fclose(infile);
  69.     if(random_flag==0 && argc==1)
  70.     {
  71.         place++;
  72.         if(place==chapters+1) place=1;
  73.         if((inoutfile=fopen("tao.ini", "wt"))==NULL) /* opens for write */
  74.         {
  75.             printf("Cannot update tao.ini.\n");
  76.             fclose(infile);
  77.             return 4;
  78.         }
  79.         fprintf(inoutfile, "%d\n%d\n%d\n%d\n%d", chapters, place, random_flag, pause_flag, screen_lines);
  80.         fclose(inoutfile);
  81.     }
  82.     return 0;
  83. }
  84.  
  85.  
  86.