gpt4 book ai didi

c - 对于用户输入的目录路径,C 中可能存在不同的错误处理吗?

转载 作者:行者123 更新时间:2023-11-30 19:34:38 25 4
gpt4 key购买 nike

      int main(int argc, char *argv[]) {
int opt= 0;
int start = -1, end = -1;
char *alg,*dir,*graph;
//Specifying the expected options
//The two options s and e expect numbers as argument
static struct option long_options[] = {
{"start",no_argument,0,'s' },
{"end",no_argument,0,'e' },
{"algorithm",no_argument, 0,'a' },
{"directory",required_argument, 0,'d' },
{"graph",required_argument,0,'g' },
{0,0,0,0}
};

int long_index =0;
int i=0,j=0;
size_t size = 1;
while ((opt = getopt_long(argc, argv,"s:e:a:d:h:g:",
long_options, &long_index )) != -1) {
switch (opt) {
case 'd' :
dir = optarg;

if (optarg == NULL)
printf("d option is must");
else if
{
// How to verify the path provided is Correct/existing
}
else
{
// Any more options of error handling on path
provided is available ?
}

用户通过选项-d传递目录路径,如何验证路径是否正确?

为此还可以包含哪些错误检查?

最佳答案

如果您尝试打开该目录中的文件,请打开它并检查它是否打开

FILE *infile=fopen("<directory>/<filename>","r");
if(infile==NULL){
printf("File did not open\n"); //implies directory or filename is invalid
}

虽然我不确定这就是你要问的。请详细说明

关于c - 对于用户输入的目录路径,C 中可能存在不同的错误处理吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43567347/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com