C++에서 파일 출력 시 파일 오류 확인은?

freesky의 이미지

질문 내용 그대로입니다.

파일에 내용을 출력하려는데 이미 해당 파일이 있는지 확인하는 부분 좀 알려 주세요..

저번에 이와 관련된 질문을 했는데 그 때 잊고 질문을 적지 못했습니다.

현재 갖고 있는 책이 부실합니다.

내용이 더 풍부한 C++ 책을 한 권 사야겠어요..

컴파일러는 Borland C++ 3.1입니다. (꽤 오래된 것이죠..)

나는오리의 이미지

CRT의 _access를 쓰세요.

#include <io.h>
...
if (_access("C:\\test.ini", 0) == -1)
    printf("파일 있다\r\n");
else
    printf("파일 있지롱~\r\n");
aswip의 이미지

</span>
 
<code class="bb-code">
 
FILE *fp = fopen&#40;&quot;c&#58;\\data.txt&quot;, &quot;rb&quot;&#41;;
printf&#40; fp ? &quot;exist.\n&quot; &#58; &quot;not exist.\n&quot; &#41;;
 
혹은
 
int nFD = open &#40;&quot;c&#58;\\data.txt&quot;, _O_RDONLY &#41;;
printf&#40; nFD &gt; 0 ? &quot;exist.\n&quot; &#58; &quot;not exist.\n&quot; &#41;;
 
</code>
 
<span>[c++ style]</span>
 
<code class="bb-code">
 
ifstream in&#40; &quot;c&#58;\\data.txt&quot;, ios&#58;&#58;in | ios&#58;&#58;binary &#41;;
printf&#40; in.is_open&#40;&#41; ? &quot;exist.\n&quot; &#58; &quot;not exist.\n&quot; &#41;;
 
</code>

- 인생은 스스로 -