向文件中写数据x,y!写数据
int x=10,y=10;
CString str="E:\\f.txt";
CFile myfile;
myfile.open(str,CFile::mode Create|CFile::ReadWrite);
myfile.write(&x,2);
myfile.write(&y,2);
//CFile::mode Create表示建立文件,若文件存在则长度设置为o
//CFile::ReadWrite 可读写
这两个为什么要组合一下?
对于写文件只用mode Create不行吗??若不存在则产生,若存在则长度设置为0。还要CFile::ReadWrite干啥用?

解决方案 »

  1.   

    CFile::mode Create|CFile::Write);
    对于写文件,这样不是就可以了
      

  2.   

    如果只写的话CFile::modeWrite
    http://msdn2.microsoft.com/en-us/library/cz0a83sb(VS.80).aspx
      

  3.   

    HANDLE hFile = ::CreateFile(pstrFileName, 
             GENERIC_WRITE, 
             0, 
             NULL,
             CREATE_ALWAYS, 
             FILE_ATTRIBUTE_NORMAL, 
             NULL);
          if( hFile == INVALID_HANDLE_VALUE ) return FALSE;
    可以参考一下,我封装的.创建文件DWORD dwWritten;
    if( !::WriteFile(hFile, "要写的内容", "要写的文件长度", &dwWritten, NULL) ) return FALSE;