Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/unix/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C-将argv[1]追加到unix路径的末尾_C_Unix - Fatal编程技术网

C-将argv[1]追加到unix路径的末尾

C-将argv[1]追加到unix路径的末尾,c,unix,C,Unix,我试图编写一个C程序来解析UNIX“PATH”变量,并检查命令行参数文件(argv[1])是否存在于该路径上的任何地址。唯一一件似乎不起作用的事情是,我尝试将“/”附加到temp,然后将argv[1]附加到temp。在while循环中,temp成为path变量的下一个子路径,然后检查命令行中输入的文件名是否存在于该路径上。将“/”附加到temp的末尾,然后将argv[1]附加到temp的末尾的最佳方式是什么 #include <unistd.h> #include <stdio

我试图编写一个C程序来解析UNIX“PATH”变量,并检查命令行参数文件(argv[1])是否存在于该路径上的任何地址。唯一一件似乎不起作用的事情是,我尝试将“/”附加到temp,然后将argv[1]附加到temp。在while循环中,temp成为path变量的下一个子路径,然后检查命令行中输入的文件名是否存在于该路径上。将“/”附加到temp的末尾,然后将argv[1]附加到temp的末尾的最佳方式是什么

#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

 void append(char* s, char c)
   {
        int len = strlen(s);
        s[len] = c;
        s[len+1] = '\0';
  }

int main(int argc, char* argv[]){
    const char* path = getenv("PATH");
    char* temp;
    FILE *file;
    char sym = '/';

    temp = strdup(path);
    temp = strtok(temp,":");

    while(temp != NULL){
        printf("%s\n",temp);
        //add "/argv[1]" to temp
        append(sym,temp);
        strcat(argv[1],temp);
        printf("%s",temp);
        file = fopen(temp,"r");
        if(file == NULL){
         printf("ERROR: File \"%s\" does not exist.",temp);
        }
        else{
           printf("Success! File \"%s\" exists.",temp);
        } 
        temp = strtok(NULL,":");
    }

    printf("%s",temp);




     return 0;
}
#包括
#包括
#包括
#包括
无效附加(字符*s,字符c)
{
int len=strlen(s);
s[len]=c;
s[len+1]='\0';
}
int main(int argc,char*argv[]){
const char*path=getenv(“路径”);
字符*温度;
文件*文件;
字符sym='/';
temp=strdup(路径);
温度=标准温度(温度,“:”);
while(temp!=NULL){
printf(“%s\n”,temp);
//将“/argv[1]”添加到临时
附加(符号、温度);
strcat(argv[1],温度);
printf(“%s”,温度);
文件=fopen(临时“r”);
if(file==NULL){
printf(“错误:文件\%s\”不存在。”,temp);
}
否则{
printf(“成功!存在文件\%s\”,temp);
} 
temp=strtok(空,“:”);
}
printf(“%s”,温度);
返回0;
}

这是一个没有“追加”功能的工作版本:

#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main( int argc, char* argv[] ) {
    char* path = getenv("PATH");
    char* temp;
    FILE *file;
    char tmp_file[100];

    temp = strtok(path,":");

    while ( temp != NULL ) {
        printf("temp: %s\n", temp);

        sprintf( tmp_file, "%s/%s", temp, argv[1] );
        printf("tmp_file: %s\n", tmp_file);

        file = fopen(tmp_file,"r");
        if ( file == NULL ) {
          printf("ERROR: File \"%s\" does not exist.\n", tmp_file);
        }
        else{
          printf("SUCCESS! File \"%s\" exists.\n", tmp_file);
        }
        temp = strtok(NULL,":");
    }

    return 0;
}
#包括
#包括
#包括
#包括
int main(int argc,char*argv[]){
char*path=getenv(“路径”);
字符*温度;
文件*文件;
char tmp_文件[100];
temp=strtok(路径“:”);
while(temp!=NULL){
printf(“临时:%s\n”,临时);
sprintf(tmp_文件,“%s/%s”,temp,argv[1]);
printf(“tmp_文件:%s\n”,tmp_文件);
文件=fopen(tmp_文件,“r”);
if(file==NULL){
printf(“错误:文件\%s\”不存在。\n”,tmp_文件);
}
否则{
printf(“成功!存在文件\%s\。\n”,tmp_文件);
}
temp=strtok(空,“:”);
}
返回0;
}

(您也可以使用
asprintf
而不是
sprintf
,但不要忘记释放分配的内存)

您不应该修改temp,因为您会干扰strtok。 代码的主要问题是管理文件名char字符串的存储。如果要构建一个新字符串,该字符串是路径“/”和argv[1”的串联,则需要一个存储空间。最简单的解决方案是为此使用数组。让它足够长

#include <stdio.h>
#include <string.h>

int main(int argc, char* argv[])
{
    char* path = strdup(getenv("PATH")); // <-- copy the path string
    char* temp;
    FILE *file;
    char fileName[1024]; // <-- Storage for the file name

    temp = strtok(path,":");
    while(temp != NULL)
    {
        printf("temp: %s\n", temp);

        sprintf(fileName, "%s/%s", temp, argv[1]);
        printf("fileName: %s\n", tmp_file);

        file = fopen(fileName,"r");
        if ( file == NULL )
            printf("ERROR: File \"%s\" does not exist.\n", fileName);
        else
            printf("SUCCESS! File \"%s\" exists.\n", fileName);

        temp = strtok(NULL,":");
    }
    free(path); // <-- delete the modified copy of path
    return 0;
}
#包括
#包括
int main(int argc,char*argv[])
{

char*path=strdup(getenv(“path”);//这里
append(argv[1],sym);
您假设
argv[1]
提供了任何附加的、可用的空间来存储更多要追加的字符。情况肯定不是这样。这里的情况与
strcat(argv[1],temp)相同
。使用
strdup
后,必须使用
free
释放分配的内存。我编辑了代码(见上文),这样我就没有向argv[1]添加任何内容并对其进行测试,但仍然没有成功,argv[1]也没有添加到temp的末尾。@szpal,我在末尾添加了它,但我的主要问题仍然是添加了argv[1]使用
temp=strtok(temp,:”;
现在使用tempNow时,您遇到的问题与以前使用
argv[]
时相同。C根本没有为您分配空间。上面的
strcat()
超出了
temp
的范围。这对我很有效,谢谢。我不知道“sprintf”如果路径被加到AGV中的长度比99个字符长,这个方法就会失败。@ Alk,谢谢:D,但是在下投票之前读一下我的最后一条评论……我希望你有一个好的大邑,不要简单地认为你的代码是“工作版本”。还有你的“最后评论”。没有指出您的方法可能存在的缺陷。而且
asprintf()
是非标准的。