我想用VC创建个文本文件,代码该如何写呢?比如我想在d:创建dve.txt的文件同时对他进行读写操作。请高手详细做答,小弟谢了!

解决方案 »

  1.   

    这个够细的了
    http://community.csdn.net/Expert/topic/3972/3972572.xml?temp=.567898
      

  2.   

    FILE *fp;
    fp = fopen("d:\\dve.txt","w");
    if(fp == NULL)
      AfxMessageBox("文件创建失败");
      

  3.   

    有n种方法:FILE *pFile=fopen("1.txt","w");
    fwrite("http://www.sunxin.org",1,strlen("http://www.sunxin.org"),pFile);
    //fseek(pFile,0,SEEK_SET);
    //fwrite("ftp:",1,strlen("ftp:"),pFile);
    //fwrite("http://www.sunxin.org",1,strlen("http://www.sunxin.org"),pFile);
    fclose(pFile);*/
    //fflush(pFile);/* FILE *pFile=fopen("2.txt","wb");
    char ch[3];
    ch[0]='a';
    ch[1]=10;
    ch[2]='b';
    fwrite(ch,1,3,pFile);
    fclose(pFile);*/ /*FILE *pFile=fopen("3.txt","w");
    int i=98341;
    char ch[5];*/
    /*ch[0]=9+48;
    ch[1]=8+48;
    ch[2]=3+48;
    ch[3]=4+48;
    ch[4]=1+48;*/
    /*itoa(i,ch,10); //fwrite(&i,4,1,pFile);
    fwrite(ch,1,5,pFile);
    fclose(pFile);*//* ofstream ofs("4.txt");
    ofs.write("http://www.sunxin.org",strlen("http://www.sunxin.org"));
    ofs.close();*/
    /* HANDLE hFile;
    hFile=CreateFile("5.txt",GENERIC_WRITE,0,NULL,CREATE_NEW,
    FILE_ATTRIBUTE_NORMAL,NULL);
    DWORD dwWrites;
    WriteFile(hFile,"http://www.sunxin.org",strlen("http://www.sunxin.org"),
    &dwWrites,NULL);
    CloseHandle(hFile);*/
    /* CFile file("6.txt",CFile::modeCreate | CFile::modeWrite);
    file.Write("http://www.sunxin.org",strlen("http://www.sunxin.org"));
    file.Close();*/取掉注释就是一种方法
      

  4.   

    1.
    CFile file("d:\\dve.txt",CFile::modeCreate|CFile::modeWrite);
    file.Write("11111111",8);
    file.Close();2.
    char buffer[17];
    CFile file("d:\\dve.txt",CFile::modeRead);
    file.Read(buffer,16);
    file.Close();
      

  5.   

    ////////////// HANDLE hFile ;
    CString str_temp;
    str_temp = _T("d:\\dve.txt");
        hFile= ::CreateFile(str_temp,
    GENERIC_READ | GENERIC_WRITE,
    0,
    NULL, 
    OPEN_ALWAYS, 
    FILE_ATTRIBUTE_NORMAL, 
    NULL);   if (hFile == INVALID_HANDLE_VALUE)
       {
          AfxMessageBox(_T("Couldn't create the file!"));
       }
    else
       {
          CFile myFile((int)hFile);      static const char sz[20] = "Hockey is best!";
          myFile.Write(sz, lstrlen(sz));
          myFile.Close();
       }
    ///////////////////