ofstream将所有内容写入我的文件两次

时间:2016-04-05 17:33:50

标签: c++ ofstream

所以我有一个简短的程序来为文件生成成本矩阵,但是当我运行它时,所有内容都会在文件中显示两次。

这就是我所拥有的:

    #include <cstdlib>
    #include <fstream>

    using namespace std;

    int main(){

      int V = rand() % 15 + 5;
      int graph[V][V];
      for( int i = 0; i < V; i++ ){
       for( int j = 0; j < V; j++ ){
         graph[i][j] = rand() % 50;
       }
      }
     for( int i = 0; i < V; i++ ){
        graph[i][i] = 0;
     }

   ofstream fout;
   fout.open( "graphTest.txt", ios::app );

   fout << V << endl;

   for( int i = 0; i < V; i++ ){
    for( int j = 0; j < V; j++ ){
      fout << graph[i][j] << ", ";
    }
    fout << endl;
  }

 fout.close();
return 0;
}

以下是文件显示的内容:

18
0, 27, 15, 43, 35, 36, 42, 49, 21, 12, 27, 40, 9, 13, 26, 40, 26, 22, 
36, 0, 18, 17, 29, 32, 30, 12, 23, 17, 35, 29, 2, 22, 8, 19, 17, 43, 
6, 11, 0, 29, 23, 21, 19, 34, 37, 48, 24, 15, 20, 13, 26, 41, 30, 6, 
23, 12, 20, 0, 31, 5, 25, 34, 27, 36, 5, 46, 29, 13, 7, 24, 45, 32, 
45, 14, 17, 34, 0, 43, 0, 37, 8, 26, 28, 38, 34, 3, 1, 4, 49, 32, 
10, 26, 18, 39, 12, 0, 36, 44, 39, 45, 20, 34, 28, 17, 1, 47, 2, 17, 
42, 2, 6, 1, 30, 36, 0, 15, 39, 44, 19, 40, 29, 31, 17, 47, 21, 31, 
25, 9, 27, 17, 6, 47, 3, 0, 15, 6, 33, 19, 24, 28, 21, 32, 29, 3, 
19, 20, 18, 8, 15, 40, 49, 46, 0, 18, 45, 46, 1, 21, 5, 29, 38, 14, 
28, 41, 0, 43, 0, 34, 14, 24, 14, 0, 6, 43, 41, 27, 15, 9, 36, 32, 
1, 37, 28, 25, 7, 24, 21, 8, 45, 29, 0, 35, 43, 18, 28, 43, 11, 28, 
29, 26, 4, 43, 13, 13, 38, 6, 40, 4, 18, 0, 38, 19, 17, 17, 46, 24, 
43, 20, 33, 40, 49, 22, 25, 44, 40, 5, 39, 4, 0, 19, 32, 42, 14, 47, 
7, 5, 4, 48, 11, 22, 28, 49, 43, 46, 18, 40, 22, 0, 10, 5, 1, 11, 
30, 28, 5, 20, 36, 44, 26, 22, 15, 8, 16, 32, 8, 24, 0, 12, 24, 0, 
36, 2, 49, 29, 0, 18, 21, 23, 31, 31, 30, 33, 44, 10, 13, 0, 31, 49, 
46, 9, 23, 13, 18, 40, 45, 26, 16, 34, 40, 40, 34, 26, 42, 36, 0, 45, 
6, 29, 18, 37, 12, 48, 22, 9, 9, 36, 10, 42, 37, 6, 1, 13, 22, 0, 
18
0, 27, 15, 43, 35, 36, 42, 49, 21, 12, 27, 40, 9, 13, 26, 40, 26, 22, 
36, 0, 18, 17, 29, 32, 30, 12, 23, 17, 35, 29, 2, 22, 8, 19, 17, 43, 
6, 11, 0, 29, 23, 21, 19, 34, 37, 48, 24, 15, 20, 13, 26, 41, 30, 6, 
23, 12, 20, 0, 31, 5, 25, 34, 27, 36, 5, 46, 29, 13, 7, 24, 45, 32, 
45, 14, 17, 34, 0, 43, 0, 37, 8, 26, 28, 38, 34, 3, 1, 4, 49, 32, 
10, 26, 18, 39, 12, 0, 36, 44, 39, 45, 20, 34, 28, 17, 1, 47, 2, 17, 
42, 2, 6, 1, 30, 36, 0, 15, 39, 44, 19, 40, 29, 31, 17, 47, 21, 31, 
25, 9, 27, 17, 6, 47, 3, 0, 15, 6, 33, 19, 24, 28, 21, 32, 29, 3, 
19, 20, 18, 8, 15, 40, 49, 46, 0, 18, 45, 46, 1, 21, 5, 29, 38, 14, 
28, 41, 0, 43, 0, 34, 14, 24, 14, 0, 6, 43, 41, 27, 15, 9, 36, 32, 
1, 37, 28, 25, 7, 24, 21, 8, 45, 29, 0, 35, 43, 18, 28, 43, 11, 28, 
29, 26, 4, 43, 13, 13, 38, 6, 40, 4, 18, 0, 38, 19, 17, 17, 46, 24, 
43, 20, 33, 40, 49, 22, 25, 44, 40, 5, 39, 4, 0, 19, 32, 42, 14, 47, 
7, 5, 4, 48, 11, 22, 28, 49, 43, 46, 18, 40, 22, 0, 10, 5, 1, 11, 
30, 28, 5, 20, 36, 44, 26, 22, 15, 8, 16, 32, 8, 24, 0, 12, 24, 0, 
36, 2, 49, 29, 0, 18, 21, 23, 31, 31, 30, 33, 44, 10, 13, 0, 31, 49, 
46, 9, 23, 13, 18, 40, 45, 26, 16, 34, 40, 40, 34, 26, 42, 36, 0, 45, 
6, 29, 18, 37, 12, 48, 22, 9, 9, 36, 10, 42, 37, 6, 1, 13, 22, 0,

我不确定为什么它会循环两次

1 个答案:

答案 0 :(得分:2)

您使用ios::app标记打开了文件,因此它将数据附加到以前运行的现有文件中:

fout.open( "graphTest.txt", ios::app );
                         // ^^^^^^^^

使用ios::trunc标志打开您的文件,覆盖之前运行的数据:

fout.open( "graphTest.txt", ios::trunc );
                         // ^^^^^^^^^^

作为旁注:

您在文件中看到与后续运行程序相同的序列的原因是因为您错过了初始化随机生成器。你必须打电话,例如。

seed(time(NULL));

main()的开头。