gpt4 book ai didi

c++ - 如何检查 zip 文件或压缩文件是否存在?

转载 作者:行者123 更新时间:2023-11-28 03:07:13 25 4
gpt4 key购买 nike

如果我不知道一个文件是否存在。如果它存在,我将根据该文件中的内容做一些事情。该文件可以是 .txt 或 .gz 格式,也可以不存在。下面是我的代码:

      //Check whether is a zip file
char *pre_pcom_file_copy = new char[strlen(pre_pat_file)+7];
strcpy (pre_pcom_file_copy,pre_pcom_file);
strcat (pre_pcom_file_copy,".gz");
char *cmd = new char[strlen("gzip -dc ")+strlen(pre_pcom_file_copy)+1];
strcpy (cmd,"gzip -dc ");
strcat (cmd,pre_pcom_file_copy);
if ( (pre_pcom = popen(cmd,"r")) == NULL ){
//Do nothing
}else{
cout << "Parsing pre.pcomments file --->" << pre_pcom_file << endl;
pcomyyin = pre_pcom;
if(_vecTime.size() > 0){
//for pre_pcom _vecTime should be empty
pcom_time = _vecTime[_vecTime.size()-1];
}
first_time = 1; // make sure tester cycle start from 0 from pcomment file
pcomyyparse ();
pclose(pre_pcom);
//delete [] cmd;
cout << "Parsing pre.pcomments file DONE!" << endl;
}

我想检查一个与 must have 文件同名的文件,例如 hello.abc,但我想检查的文件可能是 hello.abc 或 hello.gz,甚至不存在。如果我在像上面一样,当文件 hello.txt 或 hello.gz 完全不存在时出现错误。我该如何解决这个问题?

最佳答案

只需对各种可能的名称调用 fstat() 并检查产生的错误(如果有)。

关于c++ - 如何检查 zip 文件或压缩文件是否存在?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19396908/

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