Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/65.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、NetBeans 7.2中正确使用主函数参数_C_File_Netbeans 7_Main_Ubuntu 13.10 - Fatal编程技术网

在C、NetBeans 7.2中正确使用主函数参数

在C、NetBeans 7.2中正确使用主函数参数,c,file,netbeans-7,main,ubuntu-13.10,C,File,Netbeans 7,Main,Ubuntu 13.10,我很难在NetBeans 7.2上向我的程序传递目录路径,我试图做的是在项目参数上编写${OUTPUT_path}/home/vitor/rea de Trabalho/Programaço/Teste/home/vitor/Área de Trabalho/Programação/Teste是我的目录路径,我的目录中有3个.txt文件,我的程序应该通过在路径末尾添加它们的名称来读取它们,比如: strcpy(endjogos, argv[1]); strcat(endjogos, "jogos

我很难在NetBeans 7.2上向我的程序传递目录路径,我试图做的是在项目参数上编写${OUTPUT_path}/home/vitor/rea de Trabalho/Programaço/Teste/home/vitor/Área de Trabalho/Programação/Teste是我的目录路径,我的目录中有3个.txt文件,我的程序应该通过在路径末尾添加它们的名称来读取它们,比如:

strcpy(endjogos, argv[1]);
strcat(endjogos, "jogos.txt");
strcpy(endtimes, argv[2]);
strcat(endtimes, "times.txt");
strcpy(endapost, argv[3]);
strcat(endapost, "apostas.txt");
/home/vitor/Área de Trabalho/Programaço/Teste/times.txt

以下是我的代码:

int main(int argc, char *argv[]){
if(argc == 1){
    printf("ERROR: The directory's path wasn't informed.");
    exit(1);
}
else{
    char endtimes[200];
    strcpy(endtimes, argv[1]);
    strcat(endtimes, "times.txt");
    }    
FILE *caminho;
caminho = fopen(endtimes, "r");
if (!caminho){
    printf("Error trying to open file.");
    exit(1);
}
每次我尝试运行代码时,它都会显示尝试打开文件时出错。我检查了argc,它的值是4,我想这是不对的。我没有足够的使用netbeans的经验,事实上,这是我第一个使用文件的程序。你们能帮帮我吗

我正在使用Ubuntu13

谢谢你的耐心

-编辑-

我根据下面的注释对项目参数进行了更改,endtimes正在存储正确的文件路径:/home/vitor/Área de Trabalho/programmação/Teste/times.txt,但我在尝试打开文件时仍然出错。由于我使用的是Ubuntu 13,文件路径是否应该不同?

argv[0]是可执行文件的名称,如果您以文件名的形式传递了3个参数,则这些参数将分别存储在argv[1]、argv[2]和argv[3]中。因此,您可能需要以下内容:

strcpy(endjogos, argv[1]);
strcat(endjogos, "jogos.txt");
strcpy(endtimes, argv[2]);
strcat(endtimes, "times.txt");
strcpy(endapost, argv[3]);
strcat(endapost, "apostas.txt");

请注意,C数组是零索引的,因此如果argc==4,则有argv[0]、argv[1]、argv[2]、argv[3]。

如果有类似main.out-one.txt-two.txt tree.txt的程序调用,则有4个参数。因此,C将是4 argv[0]将是main.out argv[1]将为1.txt argv[2]将是2.txt
argv[3]将是tree.txt

argv[]从0开始索引,从进程参数开始,然后是1处的第一个命令行参数。那两个人在里面干什么?有效范围为0…argc-1,您使用的路径是否已存在?fopen不会为您创建它。在这一行strcpyendjogos之后,argv[2];,添加以下内容:printfargv[2];。您的路径名中嵌入了空格,很可能需要将路径用单引号或双引号括起来。print语句将让您知道实际使用的路径。您通过该编辑从根本上改变了它。你的真实代码是什么?我同意David+1。如果你有长文件名和嵌入空格,当以arg表示时,你需要dbl引用文件名。程序只需要在目录路径的末尾添加文件名times.txt、jogos.txt和apostas.txt,因此,例如,endjogos应该是这样的/home/vitor/Área de Trabalho/Programaço/Teste/jogos.txt