读取文件问题

来源:百度知道 编辑:UC知道 时间:2024/09/27 23:33:49
HANDLE hfile;
hfile=CreateFile("f:\\123.txt",GENERIC_READ,0,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);
DWORD dw;
char buf[100];
memset(buf,0,strlen(buf));
ReadFile(hfile,buf,strlen(buf),&dw,NULL);
MessageBox(buf);
CloseHandle(hfile);

HANDLE hfile;
hfile=CreateFile("f:\\123.txt",GENERIC_READ,0,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);
DWORD dw;
char buf[100];
ReadFile(hfile,buf,strlen(buf),&dw,NULL);
buf[dw]=0;
MessageBox(buf);
CloseHandle(hfile);

想请问各位高手,为什么上面一个方法没办法得到文件的内容呢?这是什么原因?

// werwe.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include<string>
#include<iostream>
#include<assert.h>
using namespace std;
extern int *a;
extern int b[];
HWND file=NULL;
void Read()
{
HANDLE hfile;
hfile=CreateFileA("E:\\123.txt",GENERIC_READ,0,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);
assert(hfile!=NULL);
DWORD dw;
char buf[100];
memset(buf,0,sizeof(buf)/sizeof(char));
if(ReadFile(hfile,(LPVOID)buf,sizeof(buf)/sizeof(char),&dw,NULL))
cout<<"Read:"<<buf<<endl;
else
cout<<"Read file error,error code:"<<GetLastError()<<endl;
CloseHandle(hfile);
}
void Write()
{
HANDLE hfile;
char buf[]="Write from Write";
hfile=CreateFileA("E:\\123.txt",GENERIC_WRITE,0,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_