2013-10-16 4 views
0

파일이 존재하는지 여부를 모른다면 그 파일의 내용에 따라 뭔가를 할 것입니다. 파일은 .txt 또는 .gz 형식 일 수 있으며 파일도 존재할 수 없습니다. 내가 예를 들어 hello.abc에 대한 있어야합니다 파일하지만 난 hello.abc 또는 hello.gz 수 검사 할 파일과 같은 이름을 가진 파일을 검사 할zip 파일 또는 압축 파일이 있는지 확인하는 방법은 무엇입니까?

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

: 다음은 내 코드입니다 또는 심지어 exist.If 내가 위의 방법으로 파일 hello.txt 또는 hello.gz 완전히 존재하지 않는 오류가있어. 어떻게 내가 이것을 해결할 수 있습니까?

+2

이것은 C++입니다. 'C'란 태그가 붙은 이유는 무엇입니까? –

+0

@JonathonReinhart 왜냐하면 나는 C++과 C를 사용하므로 양쪽에 태그를 붙였습니다. 아니면 C++ 만 넣어야합니까? –

+1

그들은 [다른 언어] (http://programmers.stackexchange.com/questions/38942/how-is-c-different-from-c)입니다. 만약 당신이''cout << ...'을 사용하고 있다면 C가 아닌 C++을 쓰고있는 것입니다. –

답변

2

다양한 가능한 이름에 대해 fstat()를 호출하고 결과 오류를 확인하십시오.

+0

코드 예제를 보여줄 수 있습니까? abc.gz가 존재하는지 테스트하고 싶습니까? –

+3

['man stat'] (http://linux.die.net/man/2/stat) –

0

Linux, BSD 또는 OSX와 같은 POSIX 시스템을 사용하는 경우 stat을 사용하여 파일이 있는지 확인할 수 있습니다. Windows의 경우 PathFileExists을 사용하십시오.

exists처럼 Boost filesystem 라이브러리를 사용하십시오.

+2

Windows에는 ['_stat'] (http://msdn.microsoft.com/en-us/library)도 있습니다./14h5k7ff.aspx). –

관련 문제