Also habe ich ein kurzes Programm, um eine Kostenmatrix zu einer Datei zu generieren, aber wenn ich es ausführe, wird alles in der Datei zweimal angezeigt.ofstream alles auf meine Datei zweimal schreiben
Hier ist, was ich habe:
#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;
}
Und hier ist es, was die Datei zeigt:
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,
Ich bin nicht sicher, warum es zweimal durch ist Looping
Sind Sie sicher, dass Sie das Programm nicht zweimal ausgeführt haben? 'fout.open (" graphTest.txt ", ios :: app);' bedeutet, dass nicht überschrieben wird, was bereits in der Datei vorhanden ist, sondern stattdessen neue Daten am Ende der Datei hinzugefügt werden. – NathanOliver
Wie von @NathanOliver gesagt, entspricht "ios :: app" dem "Anhängen" an die Datei. Edit: Ich bin mir sicher, dass dies der Fall ist, denn 'fout << V << endl;' wird nicht in einer Schleife aufgerufen und erscheint zweimal in der Ausgabe. Hoffe, das klärt die Dinge für dich auf :) –