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 / decus / RB136 / xtrac4.lzh / XTRAC4.C < prev    next >
Text File  |  1989-07-31  |  4KB  |  119 lines

  1. /*
  2.    This program was set up for the Microsoft Quick C 1.0 compiler.
  3.    Author:  David P. Maroun, 9395 Windsor Street, Chilliwack, BC,
  4.    Canada   V2P 6C5.
  5. */
  6. #include"stdio.h"
  7. main()
  8. {
  9.    char LINE[500],filename1[65],filename2[65],
  10.       SLine[6],Reply='Z',Reply2='Z',one='1',two='2';
  11.    unsigned FirstLine,LastLine,Count=0,Count2=1;
  12.    FILE *FILE1,*FILE2;
  13. printf("This program counts lines in, and extracts part of, a file.\n");
  14. /*
  15.    There may be as many as 65 535 lines in the original file from
  16.    which the extraction is made.
  17.  
  18.    The program can handle lines longer than 500 characters, but
  19.    counts them as multiple lines.  A 513 character line is treated
  20.    as two lines.
  21.  
  22.    Null characters in the original file will be lost along with other
  23.    characters following them, but other control characters are acceptable.
  24. */
  25. FILE1=NULL;FILE2=NULL;
  26. do {
  27.     printf("Enter the name of the original file.\n");
  28.     scanf("%s",filename1);
  29.     FILE1=fopen(filename1,"rb");
  30.     if(FILE1==NULL) printf("There is no such file.\n");
  31.     } while(FILE1==NULL);
  32. printf("Please wait while lines are counted.\n\n");
  33.       while(!feof(FILE1))
  34.          {
  35.          Count++;
  36. /*
  37.    The next line ensures that a blank line is not assigned the
  38.    previous line's contents.
  39. */
  40.          *LINE=NULL;
  41.          fgets(LINE,500,FILE1);
  42.          }
  43. /*
  44.    If the last line is null or consists of only a control-Z, we do not count
  45.    it.
  46. */
  47. if((strlen(LINE)==0)||((strlen(LINE)==1)&&(LINE[strlen(LINE)-1]==26))) --Count;
  48. fclose(FILE1);
  49. /*
  50.    If the user makes an error in supplying FirstLine or LastLine, the
  51.    program returns to the point marked by the label 'one'.
  52.  
  53.    We read in the numbers of the first and last lines as strings to check
  54.    for errors that prevent their use as numbers.  If there are no errors,
  55.    we convert the strings to natural numbers.
  56. */
  57. one:
  58. printf("\nThe lines in %s\nare numbered from 1 through %u.\n",filename1,Count);
  59. printf("Give the number of the first line to extract.\n");
  60.    scanf("%s",SLine);
  61.    if(((FirstLine=atoi(SLine))<=0)||(FirstLine>Count))
  62.       {printf("\nThere is an error.\n\n"); goto one;}
  63. printf("Give the number of the last line to extract.\n");
  64.    scanf("%s",SLine);
  65.    LastLine=atoi(SLine);
  66.    if((LastLine<FirstLine)||(SLine[0]=='-'))
  67.       {printf("\nThere is an error.\n\n"); goto one;}
  68. while((Reply != 'N')&&(Reply != 'Y'))
  69.    {
  70.    printf("\nYou want to extract lines %u to %u.  Right (Y or N)?\n",
  71.       FirstLine,LastLine);
  72.    Reply=toupper(getche());
  73.    }
  74. if(Reply=='N') {Reply='Z';goto one;}
  75. two: printf("\nName the file to contain the extracted lines.\n");
  76.          scanf("%s",filename2);
  77. /*
  78.    Specifying 'CON' sends the extracted lines to the screen; 'PRN'
  79.    sends them to the printer.
  80. */
  81. FILE1=fopen(filename1,"rb");
  82. FILE2=fopen(filename2,"rb");
  83. if(FILE2 != NULL)
  84. while((Reply2 != 'N')&&(Reply2 != 'Y'))
  85.       {
  86.       printf("\n%s already exists.\nOverwrite (Y or N)?\n",filename2);
  87.       Reply2=toupper(getche());
  88.       }
  89. if(Reply2=='N')
  90.    {Reply2='Z';fclose(FILE2); goto two;}
  91. if((Reply2=='Y')||(FILE2==NULL))
  92.    {
  93. /*
  94.    We send a new line character to the screen so that if the
  95.    extracted file is sent there, it will begin on a new line.
  96. */
  97.    putchar('\n');
  98.    fclose(FILE2);
  99.    FILE2=fopen(filename2,"wb");
  100.    do{
  101. /*
  102.    The following assignment prevents an error that might occur
  103.    if the program tries to read past the end of FILE1.  Setting
  104.    each line to NULL before reading the next one prevents adding
  105.    the last line in FILE1 twice to FILE2.
  106. */
  107.       *LINE=NULL;
  108.       fgets(LINE,500,FILE1);
  109.       if(Count2>=FirstLine) fprintf(FILE2,"%s",LINE);
  110.       } while((Count2++<LastLine)&&(!feof(FILE1)));
  111. /*
  112.    We next ensure there is an end-of-text character (control-Z) on FILE2.
  113. */
  114.    if((strlen(LINE)>0)&&(LINE[strlen(LINE)-1] != 26))
  115.       fprintf(FILE2,"%c",26);
  116.    }
  117. fcloseall();
  118. }
  119.