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\n", argv[0]); exit(EXIT_FAILURE); } if((in = fopen(argv[1], "r")) == NULL) { printf("fail to open the %s\n", argv[1]); exit(EXIT_FAILURE); } strcpy(name, argv[1]); strcat(name, ".copy"); if((out = fopen(name, "w")) == NULL) { printf("fail to open the %s\n", 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!\n"); 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\n", argv[0]); exit(EXIT_FAILURE); } if((in = fopen(argv[1], "r")) == NULL) { printf("fail to open %s!\n", 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!\n", 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!\n"); 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