简体   繁体   English

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

[英]How to check whether a zip file or compressed file is exist?

If i dont know whether a file is exist.If it exist i will do something depend on the content in that file.The file could be in .txt or .gz format and the file can also not exist. 如果我不知道文件是否存在。如果它存在,我会做一些取决于该文件中的内容。文件可能是.txt或.gz格式,文件也可能不存在。 Below is my code: 以下是我的代码:

      //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;
      }

I want to check a file that has the same name with a must have file for example hello.abc but the file i want to check could be hello.abc or hello.gz or even not exist.If i do in the way like above i got error when the file hello.txt or hello.gz are totally not exist.How can i solve this? 我想检查一个具有相同名称的文件,例如hello.abc,但我要检查的文件可能是hello.abc或hello.gz,甚至不存在。如果我这样做的话,就像上面那样当文件hello.txt或hello.gz完全不存在时,我收到错误。我怎么解决这个问题?

只需在各种可能的名称上调用fstat()并检查结果错误(如果有)。

If you're on a POSIX system (like Linux, BSD or OSX) then you can use stat to see if a file exists. 如果您使用的是POSIX系统(如Linux,BSD或OSX),则可以使用stat查看文件是否存在。 On Windows use PathFileExists . 在Windows上使用PathFileExists

Or use the Boost filesystem library, like its exists function. 或者使用Boost文件系统库,就像它的exists函数一样。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM