代码如下:
//ofstream outfile(T2CA(LPCTSTR(strAppXmlRoot)), ios::ate | ios::binary);
ofstream outfile("C:\\Documents and Settings\\Administrator\\桌面\\LEE\\Mfc\\Test007\\AppDemo.xml",   
                    ios::ate | ios::binary);
outfile.write(T2CA(LPCTSTR(strHead)),strHead.GetLength());

ofstream outfile1("C:\\app.xml",ios::ate | ios:: binary);
outfile1.write(T2CA(LPCTSTR(strHead)),strHead.GetLength());

outfile.close();
outfile1.close();

       HANDLE hFile=::CreateFile(_T("C:\\test.xml"),GENERIC_WRITE,0,NULL,CREATE_ALWAYS,
FILE_ATTRIBUTE_NORMAL,NULL);
if(hFile)
{
DWORD DW;
::WriteFile(hFile,LPCTSTR(strHead),strHead.GetLength(),&DW,NULL);
}
CloseHandle(hFile);问题1:outfile1能正常写入,但是上outfile写不进去,字节为0;
问题2:用api和CFile类都不能正确写入。
附,写入内容为:#define HEAD_INDEX_XMLFILE _T("<?xml version=\"1.0\" encoding=\"GB2312\"?>\n\
<AppRegInfo>\n\
<Action></Action>\n\
<AppName>ALL</AppName>\n\
<Request_Type></Request_Type>\n\
<Response_Type>NONE</Response_Type>\n\
<Sender>AnyOne</Sender >\n\
<Receiver StateType=\"OnLine\"></Receiver>\n\
</AppRegInfo>")

解决方案 »

  1.   

    不是UNICODE的话:
    CFile f;
    f.Open(_T("C:\\test.xml"),CFile::modeCreate|CFile::modeWrite);
    CStrint cstr = _T("写入内容"); 
    f.Write(cstr,cstr.GetLength());//或者指向文件缓冲区等任何地方
    f.Close();
      

  2.   

    但是用outfile1(代码中的)能正常写入。
      

  3.   

    楼主,xml文件, 或者读出的缓存, 必须要用专门的方式保存呀, 因为里面的字符,尤其是xml head,一旦转化为CString什么的 就直接断了的, 用MSXML2保存xml文件的!
      

  4.   

    #define HEAD_INDEX_XMLFILE    _T("<?xml version=\"1.0\" encoding=\"GB2312\"?>\n\
    <AppRegInfo>\n\
    <Action></Action>\n\
    <AppName>ALL</AppName>\n\
    <Request_Type></Request_Type>\n\
    <Response_Type>NONE</Response_Type>\n\
    <Sender>AnyOne</Sender >\n\
    <Receiver StateType=\"OnLine\"></Receiver>\n\
    </AppRegInfo>")
    ===================================
    就是个字符串啊,保存时把它保存为XML
      

  5.   

    写XML文件还不如用CXMLMarkup类去写,用MSXML也可以
      

  6.   

    这个知道啊,看到别人的代码就是有点疑惑用ofstream有时能写成功,有时写不成。于是就是API 和CFILE类
    试试,结果都不行。
      

  7.   

    找到原因了,原来在2005下面fstream不支持中文路径,不过还没有API 和CFILE类仍然没有解决,小遗憾。
    解决方法。http://www.cppblog.com/gohan/archive/2008/05/11/49488.html
      

  8.   

    C:\\Documents and Settings\\Administrator\\桌面\\LEE\\Mfc\\Test007\\
    这个路径存在吗?如果只有
    C:\\Documents and Settings\\Administrator\\桌面\\LEE\\Mfc\\
    程序是不能自动建立“Test007”目录的。