Friday 8 June 2018

c++ - ofstream writing everything to my file twice

Answer


So I have a short program to generate a cost matrix to a file but when I run it everything is showing up in the file twice.



Here's what I have:



    #include 
#include


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;

}


And here's what the file shows:



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,


I'm not sure why it's looping through twice

No comments:

Post a Comment

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print &q...