ÇEXEC,awk中,不工作 [英] C exec, awk, not working
问题描述
的#include<&stdio.h中GT;
#包括LT&;&stdlib.h中GT;
#包括LT&;&unistd.h中GT;
#定义MAXLINE 512
主(INT ARGC,CHAR *的argv []){
时int k;
对于(K = 0; K< = ARGC; k ++){
如果(K%2 == 0){
如果(叉()== 0){ FILE *网络连接;
FILE * FO;
INT I;
F1 = FOPEN(的argv [K],R);
FO =的fopen(TEMP.TXT,W);
如果(!FI)
返回;
焦炭LINIE [MAXLINE],* P;
对于(;){
P =与fgets(LINIE,MAXLINE,FI);
如果(P == NULL)
打破;
LINIE [MAXLINE-1] ='\\ 0';
INT K = -1;
对于(i = 0; I< MAXLINE,我++){
如果(LINIE [I] =='\\ n')K = 1;
}
对(我= K; I> = 0; --i){
fprintf中(FO,%C,LINIE [I]);
}
}
FCLOSE(FI);
FCLOSE(FO);
出口(1);}
}
其他
{
如果(叉()== 0){
EXECL(在/ usr /斌/ AWK,AWK, - F,ouk.awk的argv [K],NULL);
出口(1);
}}
} };
这里是ouk.awk文件内容
{为(i = NF; I> = 1; I - ){如果(S){S = S,$ I}其他{S = $ I}} {打印S; S =}}
基本上是我尝试做的是打造一批ARGC过程,是为偶数镜像文件中的文本,如果不从每一行重新排列字倒着,我面临的问题是
fprintf中(FO,%C,行[J]。)
块引用>不工作,当我尝试执行awk脚本我也得到一个错误
AWK:无法打开文件>
输入记录号6,文件>
源代码行数1
块引用>如果我与它完美的作品相同的文件终端只运行awk命令所以它必须有一些做的execl的命令。
还有一件事,我已经试过下面的命令来重命名TEMP.TXT INT的argv [K]
EXECL(斌/ MV,TEMP.TXT的argv [K],NULL)
但它崩溃。
如果有人可以帮助我或者给我一个很好的EXEC C命令教程的链接将是梦幻般的,非常感谢
解决方案的
为
环路超越了数组的边界(这是不确定的行为):的(K = 0; K< = ARGC; k ++){
/ * ...略... * /
F1 = FOPEN(的argv [K],R);作为阵列具有从零开始的索引,从
0
运行N-1
,其中ñ
是数组中元素的数量。的终止条件的为
必须K< ARGC
。此外,在的argv
的第一个元素是你将要排除的程序的名称:的(K = 1; K< ARGC; k ++)
调用当
EXECL()
你需要的最后一个参数转换为的char *
:EXECL(在/ usr /斌/ AWK,AWK, - F,ouk.awk的argv [K](字符*)NULL);
#include <stdio.h> #include <stdlib.h> #include <unistd.h> #define MAXLINE 512 main(int argc,char* argv[]){ int k; for (k=0; k<=argc; k++) { if (k%2==0) { if (fork()==0){ FILE *fi; FILE *fo; int i; fi=fopen(argv[k], "r"); fo=fopen("temp.txt","w"); if (!fi) return; char linie[MAXLINE],*p; for ( ; ; ) { p = fgets(linie, MAXLINE, fi); if (p == NULL) break; linie[MAXLINE-1] = '\0'; int k=-1; for (i = 0; i <MAXLINE; i++) { if (linie[i]=='\n') k=i; } for (i = k; i >= 0; --i) { fprintf(fo,"%c", linie[i]); } } fclose(fi); fclose(fo); exit(1);} } else { if (fork()==0){ execl("/usr/bin/awk","awk","-f","ouk.awk",argv[k],NULL); exit(1); } } } };
here is the ouk.awk file content
{ for (i=NF;i>=1;i--){ if(s){s=s" "$i} else{s=$i }}{print s;s=""}}
Basically what i try to do is create a number of argc processes and is the number is even to mirror the text in the file and if not to rearrange the words from every line backwards, the problem I'm facing is
fprintf(fo,"%c",line[j])
is not working and I also get an error when I try to execute the awk script
awk: can't open file > input record number 6, file > source line number 1
if I run only the awk command in terminal with the same files it works perfectly so it must have something to do with the execl command.
One more thing, I've tried the following command to rename the temp.txt int argv[k]
execl("bin/mv","temp.txt",argv[k],NULL)
but it crashes.
If anyone could help me or give me a link to a good exec c command tutorial it would be fantastic, thanks a lot
解决方案The
for
loop is going beyond the bounds of the array (which is undefined behaviour):for (k=0; k<=argc; k++) { /* ...snip... */ fi=fopen(argv[k], "r");
as arrays have zero based index, running from
0
toN-1
whereN
is the number of elements in the array. The terminating condition of thefor
must bek < argc
. Additionally, the first element inargv
is the name of the program which you will want to exclude:for (k = 1; k < argc; k++)
When invoking
execl()
you need to cast the last argument to achar*
:execl("/usr/bin/awk","awk","-f","ouk.awk",argv[k], (char*)NULL);
这篇关于ÇEXEC,awk中,不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!