[英]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.