Страницы

Translate

пятница, 29 ноября 2013 г.

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



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.



#include <stdio.h>
#include <string.h>

#define MAXFIELD 2 //maximum rows at the top and bottom of the page
#define MAXSTRING 60
#define MAXLINE 100
#define NAME 20

FILE *in, *out;
void fprint(char *pfile);
int headfile(char *pfile, int pageno);

int main(int argc, char *argv[])
{

    char file[NAME];
    char fileout[NAME]; //the output file
   
    if(argc == 1)
    {
        printf("ERROR, no arguments\n");
        return 1;
    }
    else
    {
        printf("Type the name of the output file: ");
        gets(fileout);
        out = fopen(fileout, "w");
        while(--argc > 0)
        {
            strcpy(file, *++argv);
            if((in = fopen(file, "r")) == NULL)
            {
                perror(file);
                return 1;
            }
            else
            {
                fprint(file);
                fclose(in);
            }
        }
    }
    fclose(out);
    return 0;
}

/* print the selected file */
void fprint(char *pfile)
{
    char line[MAXLINE];
    int lineno, pageno;

   
    lineno = pageno = 1;
    lineno = headfile(pfile, pageno);
    pageno++;
    while(!feof(in))
    {
        if(lineno == 1)
        {
            fprintf(out, "\f");
            lineno = headfile(pfile, pageno);
            pageno++;
        }
        fgets(line, MAXLINE-1, in);
        fputs(line,out);
        lineno++;
        if(lineno == (MAXSTRING - MAXFIELD))
            lineno = 1;
    }
    fprintf(out, "\f");
}

/* print header and blank lines */
int headfile(char *pfile, int pageno)
{
    int line = 0;
   
    while(line++ < MAXFIELD)
        fprintf(out, "\n");
    fprintf(out, "file %s, page - %d\n", pfile, pageno);
    line++;
    return line;
}

Комментариев нет:

Отправить комментарий