VC中,怎样判断一个文件是否存在?

zhangyiheng 2006-02-23 07:58:44
如果文件存在的话,怎样删除?
就是
if(fileName.Exist())
deletefile(fileName);

具体的函数是怎样的?
...全文
13466 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
OK,OPEN一下看返回值,快,准
妍妍 2006-02-24
  • 打赏
  • 举报
回复
PathFileExists
lynx090 2006-02-23
  • 打赏
  • 举报
回复
直接使用SHELL API PathFileExists()MSDN:
Determines whether a path to a file system object such as a file or directory is valid.

Syntax

BOOL PathFileExists( LPCTSTR pszPath
);
Parameters

pszPath
[in] Pointer to a null-terminated string of maximum length MAX_PATH that contains the full path of the object to verify.
Return Value

Returns TRUE if the file exists, or FALSE otherwise. Call GetLastError for extended error information.

Remarks

This function tests the validity of the path. It works only on the local file system or on a remote drive that has been mounted to a drive letter. It will return FALSE for remote file paths that begin with the Universal Naming Convention (UNC) names \\ server or \\ server\ share. It will also return FALSE if a mounted remote drive is out of service.

Example


Show Example

#include <windows.h>
#include <iostream.h>
#include "Shlwapi.h"

void main( void )
{
// Valid file path name (file is there).
char buffer_1[ ] = "C:\\TEST\\file.txt";
char *lpStr1;
lpStr1 = buffer_1;

// Invalid file path name (file is not there).
char buffer_2[ ] = "C:\\TEST\\file.doc";
char *lpStr2;
lpStr2 = buffer_2;

// Return value from "PathFileExists".
int retval;

// Search for the presence of a file with a true result.
retval = PathFileExists(lpStr1);
if(retval == 1)
{
cout << "Search for the file path of : " << lpStr1 << endl;
cout << "The file requested \"" << lpStr1 << "\" is a valid file" << endl;
cout << "The return from function is : " << retval << endl;
}

else
{
cout << "\nThe file requested " << lpStr1 << " is not a valid file" << endl;
cout << "The return from function is : " << retval << endl;
}

// Search for the presence of a file with a false result.
retval = PathFileExists(lpStr2);

if(retval == 1)
{
cout << "\nThe file requested " << lpStr2 << "is a valid file" << endl;
cout << "Search for the file path of : " << lpStr2 << endl;
cout << "The return from function is : " << retval << endl;
}
else
{
cout << "\nThe file requested \"" << lpStr2 << "\" is not a valid file" << endl;
cout << "The return from function is : " << retval << endl;
}

}

OUTPUT
==============
Search for the file path of : C:\TEST\file.txt
The file requested "C:\TEST\file.txt" is a valid file
The return from function is : 1

The file requested "C:\TEST\file.doc" is not a valid file
The return from function is : 0
Function Information

Minimum DLL Version shlwapi.dll version 4.71 or later
Custom Implementation No
Header shlwapi.h
Import library shlwapi.lib
Minimum operating systems Windows 2000, Windows NT 4.0 with Internet Explorer 4.0, Windows 98, Windows 95 with Internet Explorer 4.0


--------------------------------------------------------------------------------

© 2003 Microsoft Corporation. All rights reserved.
duyhui 2006-02-23
  • 打赏
  • 举报
回复
同意BombZhang(Love our country != Love the party)

GetFileAttributes()判断文件存在比较简单的实现方法
qiulg 2006-02-23
  • 打赏
  • 举报
回复
void DeleteFolder(LPCSTR lpDir)
{
CFileFind ff;
CString path = lpDir;

if(path.Right(1) != "\\")
path += "\\";
path += "*.*";
BOOL res = ff.FindFile(path);

while(res)
{
res = ff.FindNextFile();
if (!ff.IsDots() && !ff.IsDirectory())
DeleteFile(ff.GetFilePath());
else if (ff.IsDots())
continue;
else if (ff.IsDirectory())
{
path = ff.GetFilePath();
//是目录时继续递归,删除该目录下的文件
DeleteFolder(path.GetBuffer(path.GetLength()));
//目录为空后删除目录
RemoveDirectory(path);
}
}
ff.Close();
//最终目录被清空了,于是删除该目录
res = RemoveDirectory(lpDir);
if(res)
return;
}
qiulg 2006-02-23
  • 打赏
  • 举报
回复
bool IsExistFile(LPCSTR pszFileName)
{
WIN32_FIND_DATA FindFileData;
HANDLE hFind;

hFind = FindFirstFile(pszFileName, &FindFileData);

if (hFind == INVALID_HANDLE_VALUE)
return false;
else
{
FindClose(hFind);
return true;
}
return false;
}
BombZhang 2006-02-23
  • 打赏
  • 举报
回复
if(GetFileAttributes("filename")!=0xFFFFFFFF)
{
DeleteFile("filename");
}
xuzheng318 2006-02-23
  • 打赏
  • 举报
回复
RemoveDirectory(path)删除文件夹(注:此文件夹必须没有文件)
DeleteFile(tempFileName);删除文件
xuzheng318 2006-02-23
  • 打赏
  • 举报
回复
OpenFile函数可以满足
HFILE OpenFile(
LPCSTR lpFileName, // pointer to the filename
LPOFSTRUCT lpReOpenBuff, // pointer to the file information struct
UINT uStyle // specifies the action and attributes
);
当返回HFILE_ERROR,则说明文件不存在!
striking 2006-02-23
  • 打赏
  • 举报
回复
CFileFind finder;
static const TCHAR szFileToFind[] = _T("C:\\WINDOWS\\SYSTEM.INI");

BOOL bResult = finder.FindFile(szFileToFind);

if (bResult)
{
ok;

finder.Close();

}
else not found.

踏实每一步 2006-02-23
  • 打赏
  • 举报
回复
CFile file;
if(file.Open("1.txt",CFile::modeRead))
MessageBox("Get file");
else
MessageBox("no file");

16,471

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Web++
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧