UpdateData(true);
CFile file;
CFileException e;
if(file.Open(_T("cc.txt"),CFile::modeCreate | CFile::modeNoInherit | CFile::modeReadWrite,&e))
{
DWORD byte = file.GetLength();
char * buf;
file.Read(buf,byte);
for(int i=0;i<byte;i++)
m_str = m_str+buf[i];
file.Close();
UpdateData(false);
AfxMessageBox(_T("读文件成功!"));
}
else
{
afxDump << "File could not be opened"<<e.m_cause<<"\n";
}m_str 是一个Edit控件的CString 变量  
不知道为什么总是读不出来,而且每次我单步执行时候byte总是0
可是我文件里确实有数据。
在线等待

解决方案 »

  1.   

    char buf[1024];
    代替char *buf
      

  2.   

    buf要申请空间的,不然数据往哪放呢
      

  3.   

    不要写modeCreate啊,它会把内容冲掉的。看看说明啊。CFile::modeCreate   Directs the constructor to create a new file. If the file exists already, it is truncated to 0 length.创建一个新文件,如果文件存在,则将内容清0。
      

  4.   

    buf = (char *)malloc(1024);或char buf[1024]
      

  5.   

    正确答案是    nustchen(壁虎) duile
      

  6.   

    还有 sailor_2002(我心依旧)  zhucde(【風間苍月】)(MS_MVP)
      

  7.   

    char *buf//这只是个32bit的指针,还没有申请heap memory.//加上以下代码:
    buf = new char[byte]delete buf; //在这个函数内释放堆内存。
      

  8.   

    CFile::modeNoTruncate   //你读不出数据来问题在这里,参数不对
    buf = (char *)malloc(1024);或char buf[1024]
    //这样可以避免读到乱码,因为你没指定存储位置
      

  9.   

    没有申请内存空间,这样申请:
    DWORD byte = file.GetLength();char * buf = new char[ (int)byte ];//申请适合大小的内存空间
    ……delete buf;//事情完了后不要忘记要释放内存
      

  10.   

    DWORD byte = file.GetLength();char * buf = new char[ (unsigned long)byte ];//申请适合大小的内存空间
    ……delete buf;//事情完了后不要忘记要释放内存