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

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

 "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. 

求问求问,大神们看看吧

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

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

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

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(); //文件流关闭。。
}