我正在尝试通过设置的程序参数传递要在代码块中读取的参数名称。当我直接打开它们时,我使用
FILE * file_1 = fopen("file31.ll", "r");
FILE * file_2 = fopen("file32.ll", "r");
并且有效,因为file31和file32与文件粘贴在同一位置。但是,当我创建一个项目(并将文件放入其粘贴中)时,我尝试在参数中写入file31.ll和file32.ll,但它不起作用。进行编译时,程序会警告由于找不到目录而无法打开文件。我也尝试编写“ file31”和“ file32”,并在Windows中复制地址,例如:C:\ Users ... \ opening_files \ file31其中opening_files是项目的名称
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "LCS.h"
int main(int argc, char * argv[])
{
if (argc < 3) {
printf("Incorret number of arguments!\n");
exit(1);
}
//I print argc and argv to test if the arguments are being passed correctly
printf("argc = %d\n", argc);
for (int i=1; i<argc; i++) {
fputs(argv[i], stdout);
printf("\n");
}
//They are printed correctly
//However, when trying to open the files, the program warns that the
//directories weren't found
//Opening the files
FILE * file_1 = fopen(argv[2], "r");
FILE * file_2 = fopen(argv[3], "r");
if (file_1 == NULL || file_2 == NULL)
{
perror("Error opening file\n");
}
我试图在线阅读有关此内容的其他文章,但是我不太了解它们,因为我对编程还很陌生。
argv []的索引不正确。 argv [1]存储file_1路径,而argv [2]存储file_2路径。
FILE * file_1 = fopen(argv[1], "r");
FILE * file_2 = fopen(argv[2], "r");