[英]How to check whether a zip file or compressed file is exist?
如果我不知道文件是否存在。如果它存在,我会做一些取决于该文件中的内容。文件可能是.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;
}
我想检查一个具有相同名称的文件,例如hello.abc,但我要检查的文件可能是hello.abc或hello.gz,甚至不存在。如果我这样做的话,就像上面那样当文件hello.txt或hello.gz完全不存在时,我收到错误。我怎么解决这个问题?
只需在各种可能的名称上调用fstat()并检查结果错误(如果有)。
如果您使用的是POSIX系统(如Linux,BSD或OSX),则可以使用stat
查看文件是否存在。 在Windows上使用PathFileExists
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.