现在我已经知道一个文件的名字,那么我现在想保存这个文件到另一个文件夹中,但是首先我要判断一下这个文件夹是否有同名的文件,哪个函数可以做到这一点呢?
6 个解决方案
#1
判断一个文件是否存在
CFileFind find;
if (!find.FindFile("Yourfile"))
// 不存在
find.Close();
CFileFind find;
if (!find.FindFile("Yourfile"))
// 不存在
find.Close();
#2
WIN32_FIND_DATA FindFileData;
HANDLE FileHandle;
FIleHandle = FindFirstFile("c:\\*.*", &FindFileData);
第一个参数是设置查找格式;
第二个参数是指向WIN32_FIND_DATA结构的指针,其内容包含找到文件或子文件夹的相关信息
HANDLE FileHandle;
FIleHandle = FindFirstFile("c:\\*.*", &FindFileData);
第一个参数是设置查找格式;
第二个参数是指向WIN32_FIND_DATA结构的指针,其内容包含找到文件或子文件夹的相关信息
#3
① PathFileExist
② _access
#include <windows.h>
#include <iostream>
#include <Shlwapi.h>
#pragma comment(lib, "shlwapi.lib")
using namespace std;
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;
}
}
#4
PathFileExists
Determines whether a path to a file system object such as a file or directory is valid.
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 Values
Returns TRUE if the file exists, or FALSE otherwise. Call GetLastError for extended error information.
Requirements
Version 4.71 and later of Shlwapi.dll
Windows NT/2000: Requires Windows 2000 (or Windows NT 4.0 with Internet Explorer 4.0 or later).
Windows 95/98/Me: Requires Windows 98 (or Windows 95 with Internet Explorer 4.0 or later).
Header: Declared in Shlwapi.h.
Import Library: Shlwapi.lib.
Determines whether a path to a file system object such as a file or directory is valid.
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 Values
Returns TRUE if the file exists, or FALSE otherwise. Call GetLastError for extended error information.
Requirements
Version 4.71 and later of Shlwapi.dll
Windows NT/2000: Requires Windows 2000 (or Windows NT 4.0 with Internet Explorer 4.0 or later).
Windows 95/98/Me: Requires Windows 98 (or Windows 95 with Internet Explorer 4.0 or later).
Header: Declared in Shlwapi.h.
Import Library: Shlwapi.lib.
#5
#include <io.h>
#include <stdio.h>
#include <stdlib.h>
void main( void )
{
/* Check for existence */
if( (_access( "ACCESS.C", 0 )) != -1 )
{
printf( "File ACCESS.C exists\n" );
/* Check for write permission */
if( (_access( "ACCESS.C", 2 )) != -1 )
printf( "File ACCESS.C has write permission\n" );
}
}
#6
十分感谢!问题已解决!结贴哈哈!!
#1
判断一个文件是否存在
CFileFind find;
if (!find.FindFile("Yourfile"))
// 不存在
find.Close();
CFileFind find;
if (!find.FindFile("Yourfile"))
// 不存在
find.Close();
#2
WIN32_FIND_DATA FindFileData;
HANDLE FileHandle;
FIleHandle = FindFirstFile("c:\\*.*", &FindFileData);
第一个参数是设置查找格式;
第二个参数是指向WIN32_FIND_DATA结构的指针,其内容包含找到文件或子文件夹的相关信息
HANDLE FileHandle;
FIleHandle = FindFirstFile("c:\\*.*", &FindFileData);
第一个参数是设置查找格式;
第二个参数是指向WIN32_FIND_DATA结构的指针,其内容包含找到文件或子文件夹的相关信息
#3
① PathFileExist
② _access
#include <windows.h>
#include <iostream>
#include <Shlwapi.h>
#pragma comment(lib, "shlwapi.lib")
using namespace std;
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;
}
}
#4
PathFileExists
Determines whether a path to a file system object such as a file or directory is valid.
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 Values
Returns TRUE if the file exists, or FALSE otherwise. Call GetLastError for extended error information.
Requirements
Version 4.71 and later of Shlwapi.dll
Windows NT/2000: Requires Windows 2000 (or Windows NT 4.0 with Internet Explorer 4.0 or later).
Windows 95/98/Me: Requires Windows 98 (or Windows 95 with Internet Explorer 4.0 or later).
Header: Declared in Shlwapi.h.
Import Library: Shlwapi.lib.
Determines whether a path to a file system object such as a file or directory is valid.
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 Values
Returns TRUE if the file exists, or FALSE otherwise. Call GetLastError for extended error information.
Requirements
Version 4.71 and later of Shlwapi.dll
Windows NT/2000: Requires Windows 2000 (or Windows NT 4.0 with Internet Explorer 4.0 or later).
Windows 95/98/Me: Requires Windows 98 (or Windows 95 with Internet Explorer 4.0 or later).
Header: Declared in Shlwapi.h.
Import Library: Shlwapi.lib.
#5
#include <io.h>
#include <stdio.h>
#include <stdlib.h>
void main( void )
{
/* Check for existence */
if( (_access( "ACCESS.C", 0 )) != -1 )
{
printf( "File ACCESS.C exists\n" );
/* Check for write permission */
if( (_access( "ACCESS.C", 2 )) != -1 )
printf( "File ACCESS.C has write permission\n" );
}
}
#6
十分感谢!问题已解决!结贴哈哈!!