simulate cat command

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 
 4 int main(int argc,char **argv)
 5 {
 6     FILE *fp;
 7 
 8     //check command usage
 9     if(argc != 2)
10     {
11         fprintf(stdout,"%s","Usage: type Filename
");
12         exit(EXIT_FAILURE);
13     }
14     fp = fopen(argv[1],"r");
15     if(fp == NULL)
16     {
17         printf("Can not open %s.
",argv[1]);
18         exit(EXIT_FAILURE);
19     }
20     //output file content
21     for(;;)
22     {
23         //stop when eccounter EOF
24         if(feof(fp))
25             break;
26         putchar(getc(fp));
27     }
28     fclose(fp);
29 
30     return 0;
31 }    
dwl@dwl-virtual-machine:~$ ./type
Usage: type Filename

dwl@dwl-virtual-machine:~$ ./type cat.c
#include <stdio.h>
#include <stdlib.h>

int main(int argc,char **argv)
{
    FILE *fp;

    //check command usage
    if(argc != 2)
    {
        fprintf(stdout,"%s","Usage: type Filename ");
        exit(EXIT_FAILURE);
    }
    fp = fopen(argv[1],"r");
    if(fp == NULL)
    {
        printf("Can not open %s. ",argv[1]);
        exit(EXIT_FAILURE);
    }
    //output file content
    for(;;)
    {
        //stop when eccounter EOF
        if(feof(fp))
            break;
        putchar(getc(fp));
    }
    fclose(fp);

    return 0;
}   
原文地址:https://www.cnblogs.com/luwudang/p/9660990.html