VC 文件操作(文件查找,打开/保存,读/写,复制/删除/重命名)
时间: 2023-08-02 17:39:17 浏览: 147
您好,关于VC文件操作,以下是答案:
1. 文件查找:
在VC中,可以使用FindFile函数进行文件查找,其用法如下:
```c++
CFileFind finder;
BOOL bWorking = finder.FindFile(_T("C:\\*.txt"));
while (bWorking)
{
bWorking = finder.FindNextFile();
CString strFilename = finder.GetFileName();
// 处理文件名
}
finder.Close();
```
2. 打开/保存文件:
在VC中,可以使用CFileDialog类进行文件的打开和保存,其用法如下:
```c++
// 打开文件
CFileDialog dlg(TRUE);
if (dlg.DoModal() == IDOK)
{
CString strFilename = dlg.GetPathName();
// 处理文件名
}
// 保存文件
CFileDialog dlg(FALSE);
if (dlg.DoModal() == IDOK)
{
CString strFilename = dlg.GetPathName();
// 处理文件名
}
```
3. 读/写文件:
在VC中,可以使用CFile类进行文件的读写,其用法如下:
```c++
// 读文件
CFile file;
if (file.Open(_T("test.txt"), CFile::modeRead))
{
char buffer[1024];
UINT nRead = file.Read(buffer, sizeof(buffer));
CString strContent(buffer, nRead);
// 处理文件内容
file.Close();
}
// 写文件
CFile file;
if (file.Open(_T("test.txt"), CFile::modeCreate | CFile::modeWrite))
{
CString strContent = _T("Hello, world!");
file.Write(strContent, strContent.GetLength());
file.Close();
}
```
4. 复制/删除/重命名文件:
在VC中,可以使用CFile::Copy、CFile::Remove和CFile::Rename函数进行文件的复制、删除和重命名,其用法如下:
```c++
// 复制文件
CFile::Copy(_T("test.txt"), _T("test_copy.txt"), TRUE);
// 删除文件
CFile::Remove(_T("test.txt"));
// 重命名文件
CFile::Rename(_T("test.txt"), _T("test_new.txt"));
```
以上就是关于VC文件操作的一些常用方法,希望对您有所帮助。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)