윈도우 프로그래밍/C, C++, MFC
C/C++ 파일 존재 유무 체크
리게인
2022. 9. 15. 15:53
300x250
반응형
이번 예제는 현재 위치에 파일이 있는지 확인하는 코드 입니다.
CString full_path = GetExecutedPath() + _T("test/file.tmp");
if (INVALID_FILE_ATTRIBUTES == GetFileAttributes(full_path) && GetLastError() == ERROR_FILE_NOT_FOUND)
{
AfxMessageBox(_T("파일 X"));
}
else
{
AfxMessageBox(_T("파일 O"));
}
GetExcutedPath() 함수는 실행파일이 있는 위치를 리턴해 줍니다.
저걸 사용안하면 프로젝트 소스가 있는 위치에서 파일을 확인하기 때문에 ㅋ
아래 함수를 그대로 복사해서 붙여넣기 하시면 됩니다.
CString GetExecutedPath()
{
//실행파일 경로 구하는 함수
CString strResult;
CString strPath;
if (GetModuleFileName(nullptr, strPath.GetBuffer(_MAX_PATH + 1), MAX_PATH) != FALSE)
{
strPath.ReleaseBuffer();
strResult = strPath.Left(strPath.ReverseFind('\\') + 1);
}
return strResult;
}

간단하쥬 ㅋ
300x250
반응형