躲地缝…
学习了这么久C语言,也使了挺久Linux,本人才刚刚学会如何调用不同文件里面的程序(分开编译前提)。
个人觉得是个好招,要记一下。
首先编译一个upper,上代码:
#include<stdio.h>
#include<ctype.h>
#include<stdlib.h>
int main(){
int ch;
while ((ch = getchar()) != EOF) {
putchar(toupper(ch));
}
exit(0);
}
大绝招在这:
#include<unistd.h>
#include<stdio.h>
#include<stdlib.h>
int main(int argc, char *argv[])
{
char *filename;
if (argc != 2) {
fprintf(stderr,"usage: useupper file\n");
exit(1);
}
filename = argv[1];
if(!freopen(filename, "r", stdin)){
fprintf(stderr, "could not redirect stdin from file %s\n", filename);
exit(2);
}
execl("./upper","upper", NULL); //这是关键啦!!!!!!!!
perror("could not exec ./upper");
exit(3);
}