1、
[root@centos79 test]# cat test.c #include <stdio.h> #include <stdlib.h> #include <string.h> int main(int argc, char * argv[]) { char ch; FILE * in, * out; char name[30]; if(argc < 2) { printf("usage: %s filename ", argv[0]); exit(EXIT_FAILURE); } if((in = fopen(argv[1], "r")) == NULL) { printf("fail to open the %s ", argv[1]); exit(EXIT_FAILURE); } strcpy(name, argv[1]); strcat(name, ".copy"); if((out = fopen(name, "w")) == NULL) { printf("fail to open the %s ", name); exit(EXIT_FAILURE); } while((ch = getc(in)) != EOF) { putc(ch, out); } if(fclose(in) != 0 || fclose(out) != 0) { printf("fail to close the files! "); exit(EXIT_FAILURE); } return 0; }
[root@centos79 test]# ls plink.map test.c [root@centos79 test]# gcc test.c [root@centos79 test]# ls a.out plink.map test.c [root@centos79 test]# ./a.out usage: ./a.out filename [root@centos79 test]# ./a.out plink.map [root@centos79 test]# ls a.out plink.map plink.map.copy test.c [root@centos79 test]# md5sum * 3f46af47fb12b6a73fb4134f083529bf a.out 3ac45c2965cc65f5973df626dfdd016c plink.map 3ac45c2965cc65f5973df626dfdd016c plink.map.copy d346acecc65fb125312424cdaf9f2ef3 test.c
2、
[root@centos79 test]# cat test.c #include <stdio.h> #include <stdlib.h> int main(int argc, char * argv[]) { char ch; FILE * in, * out; char name[50]; if(argc < 2) { printf("usage: %s filename ", argv[0]); exit(EXIT_FAILURE); } if((in = fopen(argv[1], "r")) == NULL) { printf("fail to open %s! ", argv[1]); exit(EXIT_FAILURE); } printf("please input the out name:"); scanf("%s", name); if((out = fopen(name, "w")) == NULL) { printf("fail to open %s! ", name); exit(EXIT_FAILURE); } while((ch = getc(in)) != EOF) { putc(ch, out); } if(fclose(in) != 0 || fclose(out) != 0) { printf("fail to close the files! "); exit(EXIT_FAILURE); } return 0; }
[root@centos79 test]# ls plink.map test.c [root@centos79 test]# gcc test.c [root@centos79 test]# ls a.out plink.map test.c [root@centos79 test]# ./a.out usage: ./a.out filename [root@centos79 test]# ./a.out plink.map please input the out name:plink.map.bak [root@centos79 test]# ls a.out plink.map plink.map.bak test.c [root@centos79 test]# md5sum * c2f089ea14f8579de89af7c9a76c87e4 a.out 3ac45c2965cc65f5973df626dfdd016c plink.map 3ac45c2965cc65f5973df626dfdd016c plink.map.bak d598967c6b77d67aae7a362f6b8e8b01 test.c
3、
[root@centos79 test]# cat test.c #include <stdio.h> #include <stdlib.h> int main(int argc, char * argv[]) { char ch; FILE * in, * out; if(argc != 3) { printf("usage: %s file1 file2 ", argv[0]); exit(EXIT_FAILURE); } if((in = fopen(argv[1], "r")) == NULL) { printf("fail to open %s! ", argv[1]); exit(EXIT_FAILURE); } if((out = fopen(argv[2], "w")) == NULL) { printf("fail to open %s! ", argv[2]); exit(EXIT_FAILURE); } while((ch = getc(in)) != EOF) { putc(ch, out); } if(fclose(in) != 0 || fclose(out) != 0) { printf("fail to open the files! "); exit(EXIT_FAILURE); } return 0; }
[root@centos79 test]# ls plink.map test.c [root@centos79 test]# gcc test.c [root@centos79 test]# ls a.out plink.map test.c [root@centos79 test]# touch temp [root@centos79 test]# ls a.out plink.map temp test.c [root@centos79 test]# ll -h total 1.3M -rwxr-xr-x. 1 root root 8.6K Oct 7 22:04 a.out -rw-r--r--. 1 root root 1.3M Oct 7 21:32 plink.map -rw-r--r--. 1 root root 0 Oct 7 22:04 temp -rw-r--r--. 1 root root 597 Oct 7 22:01 test.c [root@centos79 test]# ./a.out usage: ./a.out file1 file2 [root@centos79 test]# ./a.out plink.map temp [root@centos79 test]# ll -h total 2.5M -rwxr-xr-x. 1 root root 8.6K Oct 7 22:04 a.out -rw-r--r--. 1 root root 1.3M Oct 7 21:32 plink.map -rw-r--r--. 1 root root 1.3M Oct 7 22:04 temp -rw-r--r--. 1 root root 597 Oct 7 22:01 test.c [root@centos79 test]# md5sum * 96b5a0772f601a2ef3948b1a18395355 a.out 3ac45c2965cc65f5973df626dfdd016c plink.map 3ac45c2965cc65f5973df626dfdd016c temp 8ce9555245854e55301b63129700ff6e test.c