文件操作。使用fork()execvp()、wait/waitpid()接受单个命令行参数的c程序
我正在编写一个程序,可以在c程序中传入命令行参数。linux命令存储在名为“示例输入”的文件中。它只包含cp文件1 dest1或mv文件2 dest2。我的问题是当我标记文件中的字符串时。似乎它只读取文件的第一行。这是我的密码。有人能帮我吗文件操作。使用fork()execvp()、wait/waitpid()接受单个命令行参数的c程序,c,shell,C,Shell,我正在编写一个程序,可以在c程序中传入命令行参数。linux命令存储在名为“示例输入”的文件中。它只包含cp文件1 dest1或mv文件2 dest2。我的问题是当我标记文件中的字符串时。似乎它只读取文件的第一行。这是我的密码。有人能帮我吗 error message i got (after edit): temp1/f4.txt temp1/f5.txt temp1/f6.txt -temp1/f7.c +temp1/f8.c +temp2/f1.txt temp2/f2.c
error message i got (after edit):
temp1/f4.txt
temp1/f5.txt
temp1/f6.txt
-temp1/f7.c
+temp1/f8.c
+temp2/f1.txt
temp2/f2.c
+temp2/f3.txt
temp2/f4.txt
+temp2/f5.txt
temp2/f6.txt
temp2/f9.c
Test Result: [Failed]
my sample-input
cp temp1/f1.txt temp2/
mv temp1/f2.c temp2/
cp temp1/f3.txt temp2/
cp temp1/f4.txt temp2/
cp temp1/f5.txt temp2/
cp temp1/f6.txt temp2/
mv temp1/f7.c temp1/f8.c
mv temp1/f9.c temp2/
cp temp1/f1.txt temp1/f1a.txt
我现在的代码
/* Your code goes here */
#define _GNU_SOURCE
#include <string.h>
# include <stdio.h>
# include <stdlib.h>
# include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
int main(int argc, char* argv[])
{
FILE* fp;
fp = fopen("sample-input", "r");
if (fp == NULL) {
fprintf(stderr, "An error has occured1\n");
exit(1);
}
if (argc != 2) {
fprintf(stderr, "argc=%d\n", argc);
fprintf(stderr, "An error has occured2 \n");
exit(1);
}
else if (argc == 2) {
char ag[1024];
while (fgets(ag, 1024, fp) != NULL)
{
fgets(ag, 1024, fp);
char* array[4];
int i = 0;
char* token = strtok(ag, " \t\n");
while (token != NULL) {
array[i] = token;
i++;
token = strtok(NULL, " \t\n");
}
array[i] = NULL;
int r = fork();
if (r == 0) {
execvp(array[0], array);
}
else if (r < 0) {
fprintf(stderr, "An error an occoured\n");
exit(1);
}
else {
wait(NULL);
}
}
}
return 0;
}
/*您的代码在这里*/
#定义GNU源
#包括
#包括
#包括
#包括
#包括
#包括
int main(int argc,char*argv[])
{
文件*fp;
fp=fopen(“样本输入”,“r”);
如果(fp==NULL){
fprintf(stderr,“发生错误1\n”);
出口(1);
}
如果(argc!=2){
fprintf(标准字符,“argc=%d\n”,argc);
fprintf(stderr,“发生错误2\n”);
出口(1);
}
else if(argc==2){
char-ag[1024];
while(fgets(ag,1024,fp)!=NULL)
{
fgets(ag,1024,fp);
字符*数组[4];
int i=0;
char*token=strtok(ag,“\t\n”);
while(令牌!=NULL){
数组[i]=令牌;
i++;
令牌=strtok(空,“\t\n”);
}
数组[i]=NULL;
int r=fork();
如果(r==0){
execvp(数组[0],数组);
}
else如果(r<0){
fprintf(stderr,“一个偶然的错误”);
出口(1);
}
否则{
等待(空);
}
}
}
返回0;
}
这个
一定是
execvp(array[0], array);
因为第一个参数应该是可执行文件的文件名。您应该通过发出消息并退出(!)来正确处理execvp()
失败的情况
此外,您必须通过设置
array[i] = NULL;
在你的while循环之后。最好添加一个检查,它在解析三个参数后中断循环,否则array
会溢出(写越界导致未定义的行为)
另一个问题是您的读取循环
while (fgets(ag, 1024, fp) != NULL) {
fgets(ag, 1024, fp);
}
这会让你疯狂地阅读文件,留下最后一行或上一行。您必须将行处理放在循环中:
while (fgets(ag, 1024, fp) != NULL) {
// Parse arguments and fork/exec here inside
}
根据您的输入文件,制表器可能也是一个参数分隔符,所以请使用
strtok(ag, " \t\n");
及
在编辑我的程序后,仍然会出现同样的错误。看起来文件没有被复制并移动到目标文件夹。你能看一下吗?谢谢你的帮助。你能编辑你的问题中的信息吗?格式正确,preferably@DeZeng可能是制表员出了问题,请参阅更新的答案。我一直在检查temp2文件,但似乎什么都没有移动到它。请确保您解决了我上面提出的所有问题。如果您有,那么请在原始代码的基础上添加您的当前代码(不要删除此代码)
while (fgets(ag, 1024, fp) != NULL) {
// Parse arguments and fork/exec here inside
}
strtok(ag, " \t\n");
strtok(NULL, " \t\n");