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 >
Wrap
Text File
|
1989-07-31
|
4KB
|
119 lines
/*
This program was set up for the Microsoft Quick C 1.0 compiler.
Author: David P. Maroun, 9395 Windsor Street, Chilliwack, BC,
Canada V2P 6C5.
*/
#include"stdio.h"
main()
{
char LINE[500],filename1[65],filename2[65],
SLine[6],Reply='Z',Reply2='Z',one='1',two='2';
unsigned FirstLine,LastLine,Count=0,Count2=1;
FILE *FILE1,*FILE2;
printf("This program counts lines in, and extracts part of, a file.\n");
/*
There may be as many as 65 535 lines in the original file from
which the extraction is made.
The program can handle lines longer than 500 characters, but
counts them as multiple lines. A 513 character line is treated
as two lines.
Null characters in the original file will be lost along with other
characters following them, but other control characters are acceptable.
*/
FILE1=NULL;FILE2=NULL;
do {
printf("Enter the name of the original file.\n");
scanf("%s",filename1);
FILE1=fopen(filename1,"rb");
if(FILE1==NULL) printf("There is no such file.\n");
} while(FILE1==NULL);
printf("Please wait while lines are counted.\n\n");
while(!feof(FILE1))
{
Count++;
/*
The next line ensures that a blank line is not assigned the
previous line's contents.
*/
*LINE=NULL;
fgets(LINE,500,FILE1);
}
/*
If the last line is null or consists of only a control-Z, we do not count
it.
*/
if((strlen(LINE)==0)||((strlen(LINE)==1)&&(LINE[strlen(LINE)-1]==26))) --Count;
fclose(FILE1);
/*
If the user makes an error in supplying FirstLine or LastLine, the
program returns to the point marked by the label 'one'.
We read in the numbers of the first and last lines as strings to check
for errors that prevent their use as numbers. If there are no errors,
we convert the strings to natural numbers.
*/
one:
printf("\nThe lines in %s\nare numbered from 1 through %u.\n",filename1,Count);
printf("Give the number of the first line to extract.\n");
scanf("%s",SLine);
if(((FirstLine=atoi(SLine))<=0)||(FirstLine>Count))
{printf("\nThere is an error.\n\n"); goto one;}
printf("Give the number of the last line to extract.\n");
scanf("%s",SLine);
LastLine=atoi(SLine);
if((LastLine<FirstLine)||(SLine[0]=='-'))
{printf("\nThere is an error.\n\n"); goto one;}
while((Reply != 'N')&&(Reply != 'Y'))
{
printf("\nYou want to extract lines %u to %u. Right (Y or N)?\n",
FirstLine,LastLine);
Reply=toupper(getche());
}
if(Reply=='N') {Reply='Z';goto one;}
two: printf("\nName the file to contain the extracted lines.\n");
scanf("%s",filename2);
/*
Specifying 'CON' sends the extracted lines to the screen; 'PRN'
sends them to the printer.
*/
FILE1=fopen(filename1,"rb");
FILE2=fopen(filename2,"rb");
if(FILE2 != NULL)
while((Reply2 != 'N')&&(Reply2 != 'Y'))
{
printf("\n%s already exists.\nOverwrite (Y or N)?\n",filename2);
Reply2=toupper(getche());
}
if(Reply2=='N')
{Reply2='Z';fclose(FILE2); goto two;}
if((Reply2=='Y')||(FILE2==NULL))
{
/*
We send a new line character to the screen so that if the
extracted file is sent there, it will begin on a new line.
*/
putchar('\n');
fclose(FILE2);
FILE2=fopen(filename2,"wb");
do{
/*
The following assignment prevents an error that might occur
if the program tries to read past the end of FILE1. Setting
each line to NULL before reading the next one prevents adding
the last line in FILE1 twice to FILE2.
*/
*LINE=NULL;
fgets(LINE,500,FILE1);
if(Count2>=FirstLine) fprintf(FILE2,"%s",LINE);
} while((Count2++<LastLine)&&(!feof(FILE1)));
/*
We next ensure there is an end-of-text character (control-Z) on FILE2.
*/
if((strlen(LINE)>0)&&(LINE[strlen(LINE)-1] != 26))
fprintf(FILE2,"%c",26);
}
fcloseall();
}