#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#define MAXLINE 512
main(int argc,char* argv[]){
int k;
for (k=1; k<=argc; k++) {
if ((k%2)==0) {
if (fork()==0){
printf("asdsa");
int fd; /*file descriptor to the file we will redirect ls's output*/
if((fd = open("file1.txt", O_RDWR | O_CREAT))==-1){ /*open the file */
perror("open");
return 1;
}
dup2(fd,STDOUT_FILENO); /*copy the file descriptor fd into standard output*/
dup2(fd,STDERR_FILENO); /* same, for the standard error */
close(fd); /* close the file descriptor as we don't need it more */
/*execl ls */
execl("/usr/bin/rev","rev",argv[k],NULL);
exit(1);
}
else
{
wait(0);
}
char temp[512];
sprintf(temp, "mv file1.txt %s",argv[k]);
system((char *)temp);
}
else
{
if (fork()==0) {
int fd; /*file descriptor to the file we will redirect ls's output*/
if((fd = open("file2.txt", O_RDWR | O_CREAT))==-1){ /*open the file */
perror("open");
return 1;
}
dup2(fd,STDOUT_FILENO); /*copy the file descriptor fd into standard output*/
dup2(fd,STDERR_FILENO); /* same, for the standard error */
close(fd); /* close the file descriptor as we don't need it more */
execl("/usr/bin/awk","awk","-f","awk2.sh",argv[k],NULL);
exit(1);
}
else
{
wait(0);
}
char temp[512];
sprintf(temp, "mv file2.txt %s",argv[k]);
system((char *)temp);
}
}
}
我尝试做的是将文件 file2.txt 重命名为argv[k]
,但它不起作用。基本上整个想法是将excel
命令的结果保存在相同的文件 argv[k] 中。谁能帮我这个?
编辑:argv[]
是在命令行中作为参数给出的文件列表,例如“file1.txt”“file5.txt”等。
Edit2:让我们假装我像这样运行
$gcc program.c
$./a.out myfile.txt
myfile.txt
abc
efg
ouk2.awk 所做的是保存在
file2.txt
cba
gfe
现在我接下来想要的是用 file2.txt 的内容重写 myfile.txt 所以
myfile.txt
cba
gfe
但是这最后一步不起作用,它应该由系统命令完成,但由于某种原因,文件“myfile.txt”没有任何变化,在我启动程序后,我得到的只是
myfile.txt
abc
efg
file2.txt
cba
gfe
在我想要得到之前我是怎么说的
myfile.txt
cba
gfe