如果文本文件的内容是:12345
而我重新写入abc,这样变成abc45,但是我想写入abc,45要去掉的,但是没有东西能覆盖,所以45还保留着,该怎么处理,使45去掉?

解决方案 »

  1.   

    跟你打开文件的模式有关。比如,用fopen打开文本文件:
    FILE fp = fopen("test.txt", "wt");
    这样写文件以后,以前的内容就不存在了。
      

  2.   

    First delete it
    and then write!
      

  3.   

    把存取模式从"w"改为"w+"不行的,我已经试过了,还是变成abc45而不是楼主想要的abc,实现的代码应该如下:
    FILE *pFile=fopen("1.txt","w");
    fwrite("12345",1,strlen("12345"),pFile);

    char *pBuf;
    int len=ftell(pFile);
    pBuf=new char[len+1];
    rewind(pFile);
        fread(pBuf,1,len,pFile);
    for(int i=0;i<=len;i++)
    pBuf[i]=' ';
    fclose(pFile);

    fopen("1.txt","w");
    fseek(pFile,0,SEEK_SET);
    fwrite("abc",1,strlen("abc"),pFile);
    fclose(pFile);
    楼主不妨一试!