qq_36584323
qq_36584323
2016-12-23 02:58
采纳率: 100%
浏览 1.4k
已采纳

c++输出到文件中保存不全

为什么每次只保存最后一次输出到文件中的呢,其他的不保存呢,是因为每次打开时,都将原来的清空了吗?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

6条回答 默认 最新

  • shen_wei
    shen_wei 2016-12-23 06:53
    已采纳
     "r" 
    Opens for reading. If the file does not exist or cannot be found, the fopen call fails. 
    "w" 
    Opens an empty file for writing. If the file exists, its contents are destroyed. 
    "a" 
    Opens for writing at the end of the file (appending) without removing the EOF marker before writing new data to the file; creates the file first if it doesn't exist. 
    "r+" 
    Opens for both reading and writing. (The file must exist.) 
    "w+" 
    Opens an empty file for both reading and writing. If the file exists, its contents are destroyed. 
    "a+" 
    Opens for reading and appending; the appending operation includes the removal of the EOF marker before data is written to the file and the EOF marker is restored after writing is complete; creates the file first if it doesn't exist. 
    
    点赞 评论
  • qq_36584323
    qq_36584323 2016-12-23 03:15

    求问求问,大神们看看吧

    点赞 评论
  • zhenxiaohui
    裸奔人生 2016-12-23 03:19

    应该是每次打开的时候,你都清空了吧,需要打开追加写。

    点赞 评论
  • shihengzhen101
    AlbertS 2016-12-23 05:19

    你可以查一下"输出到文件 追加",一般以“w”形式打开,你可以试试“a”模式

    点赞 评论
  • SCaryon
    SCaryon 2016-12-23 06:34

    w形式每次会把打开的文件内容清空然后再写。

    点赞 评论
  • shen_wei
    shen_wei 2016-12-26 04:09
    ofstream ocout;
    
    void AddEdge(int v1, int v2)//加边 
    { 
        ocout.seekp(0, ios_base::end);  //设置位置,每次都从txt最后位置开始。。
        cout<<"冗余边"<<v1<<" "<<v2<<endl; 
        ocout<<"冗余边"<<v1<<" "<<v2<<endl;
    
    }
    
    int main()
    { 
        ocout.open("city.txt");   //每次都重新建立txt,所以当全局变量
        for(int k=0;k<10;k++)
        { 
            AddEdge(rand()%5,rand()%5); 
        } 
        ocout.close(); //文件流关闭。。
    }  
    
    点赞 评论

相关推荐