Exercise 7-8 (Print files)
Chapter_7 Exercise_7-7 | system Exercise_7-9 |
Exercise 7-8 K&R, p. 165
Exercise 7-8. Write a program to print a set of files, starting each new one on a new page, with a title and a running page count for each file.
Note: See also cat_(concatenate).
catn.c K&R, p. 162-165 download
#include <stdio.h> // for fprintf(), fopen(), fclose(), ferror(),
// getc(), putc(), printf(), stdin, stdout, stderr, FILE, EOF, NULL
#include <stdlib.h> // for exit()
#define PAGELINES 10 // no of lines in a page
#define MAXLINE 1000
void filecopy(FILE *, FILE *);
void prntitle(char *); // print title
// concatenate files, print title and page numbers
int main(int argc, char *argv[])
{
FILE *fp;
char *prog = argv[0]; // program name
if (argc == 1) // no command-line args, just program name
{ // copy standard input to standard output
prntitle("stdin");
filecopy(stdin, stdout);
exit(0); // end program normally
}
// else
while (--argc > 0) // for all args except program name
{ // open file for reading
if ((fp = fopen(*++argv, "r")) == NULL)
{
fprintf(stderr, "%s: can't open \"%s\"\n", prog, *argv);
exit(1); // end program, signalling error
}
// else // fp != NULL, file open
prntitle(*argv);
filecopy(fp, stdout);
fclose(fp); // close file, free fp
}
if (ferror(stdout))
{
fprintf(stderr, "%s: error writing stdout\n", prog);
exit(2); // end program, signal other error code
}
exit(0); // end program normally
}
void prntitle(char *title) // print title
{
printf("----------");
printf(" %s ", title);
printf("----------\n");
}
// get at most n-1 chars from fp:
char * fGets(char *s, int n, FILE *fp); // fgets() is in stdio.h
// put string s on file fp:
int fPuts(char *s, FILE *fp); // fputs() is in stdio.h
// copy from ifp (input) to ofp (output)
void filecopy(FILE *ifp, FILE *ofp)
{
int lineno = 0;
int pageno = 0;
char line[MAXLINE];
while (fGets(line, MAXLINE, ifp) != NULL)
{
lineno++;
if (lineno % PAGELINES == 1)
{
pageno++;
fprintf(ofp, "\nPage %d:\n", pageno);
}
fPuts(line, ofp);
}
putc('\n', ofp);
}
// get at most n-1 chars from fp
char * fGets(char *s, int n, FILE *fp) // fgets() is in stdio.h
{
register int c;
register char *cs;
cs = s;
while (--n > 0 && (c = getc(fp)) != EOF)
{
if ((*cs++ = c) == '\n')
{break;}
}
*cs = '\0';
return (c == EOF && cs == s) ? NULL : s;
}
// put string s on file fp:
int fPuts(char *s, FILE *fp) // fputs() is in stdio.h
{
int c;
while (c = *s++)
{putc(c, fp);}
return ferror(fp) ? EOF : 0;
}
/*
gcc catn.c -o catn
./catn // copy the text (15 lines with the newline at the end)
The Rain in Spain
Alan Jay Lerner
The rain in Spain stays mainly in the plain!
I think she's got it! I think she's got it!
The rain in Spain stays mainly in the plain!
By George, she's got it! By George, she's got it!
Now, once again where does it rain?
On the plain! On the plain!
And where's that soggy plain?
In Spain! In Spain!
Now once again, where does it rain?
On the plain! On the plain!
And where's that blasted plain?
In Spain! In Spain!
---------- stdin ----------// paste the text after this line in the terminal window
The Rain in Spain
Alan Jay Lerner
The rain in Spain stays mainly in the plain!
I think she's got it! I think she's got it!
The rain in Spain stays mainly in the plain!
By George, she's got it! By George, she's got it!
Now, once again where does it rain?
On the plain! On the plain!
And where's that soggy plain?
In Spain! In Spain!
Now once again, where does it rain?
On the plain! On the plain!
And where's that blasted plain?
In Spain! In Spain!
Page 1:
The Rain in Spain
Alan Jay Lerner
The rain in Spain stays mainly in the plain!
I think she's got it! I think she's got it!
The rain in Spain stays mainly in the plain!
By George, she's got it! By George, she's got it!
Now, once again where does it rain?
On the plain! On the plain!
And where's that soggy plain?
In Spain! In Spain!
Page 2:
Now once again, where does it rain?
On the plain! On the plain!
And where's that blasted plain?
In Spain! In Spain!
// Ctrl^D in Linux, Ctrl^Z+Enter in Windows (EOF)
./catn cat1.c
---------- cat1.c ----------
Page 1:
..........
./catn cat1.c cat2.c
*/
Chapter_7 Exercise_7-7 | BACK_TO_TOP | system Exercise_7-9 |
Comments
Post a Comment