C++ 文件怎么就写不进去 WriteFile() 高手指点一二,谢谢

rain188si 2008-11-27 02:53:21

#include "stdafx.h"
#include <vector>
#include <Windows.h>
#include <stdio.h>

using namespace std;

int parFile( LPCTSTR pPath )
{
CFileFind finder;
CTime tempTime;
DWORD dwBytesRead, dwBytesWritten;

// 创建txt文件并把信息写入
HANDLE hFile = CreateFile( _T("c:\\MyText.txt"),
GENERIC_WRITE,
0,
NULL,
CREATE_ALWAYS,
FILE_ATTRIBUTE_NORMAL|
FILE_FLAG_OVERLAPPED,
NULL);
if (hFile == INVALID_HANDLE_VALUE)
{
printf("Could not open file(error %d)!", GetLastError());
return 0;
}

// Start work for file.
CString str;
CString cTime;
CString Path;
int sLength;
char* buf = NULL;
BOOL bWorking = finder.FindFile(pPath);
while (bWorking)
{
bWorking = finder.FindNextFile();

if (finder.IsDots())
{
continue;
}
if (finder.IsDirectory())
{
str = finder.GetFileName();
sLength = finder.GetLength();
if (finder.GetCreationTime(tempTime))
{
cTime = tempTime.Format(_T("%c"));
}
Path = finder.GetFilePath();
}

WriteFile(_T("c:\\MyText.txt"),str,sizeof(str),0,NULL);
}
finder.Close();

return 0;
}
这是一个函数,我要读一个路径下的所有文件名字,创建时间然后写入一个文档,为什么写不进去,麻烦高手指点一二,或者给个完整的程序在2005下跑。
...全文
187 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
roadblossom 2008-11-27
  • 打赏
  • 举报
回复
up
就呆在云上 2008-11-27
  • 打赏
  • 举报
回复
你的
WriteFile(_T("c:\\MyText.txt"),str,sizeof(str),0,NULL); 


貌似不对
str的长度不是这么确定的,如下:
WriteFile(_T("c:\\MyText.txt"),str,strlen(str)+1,0,NULL); 

要用:
strlen(str)+1来正确的写入字符串的长度

65,211

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

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