2016-12-17 4 views
-1

C++で書かれたコードは、一連の数値を出力コンソールに書き込みます。実際、このコードは、ソースノードと宛先ノードの間のグラフ内のすべてのパスを列挙しようとします。例えば、単一の実行は、生成します。Graph :: printAllPaths

1,2,3

4,5

6,7

を全体のコードはここで見つけることができます:

http://www.geeksforgeeks.org/find-paths-given-source-destination/

私がしようとしているのは、これらの数字をテキストファイルまたはExcelファイルに書き込むことです。私はC++を全く新しくしていますので、Excelファイルやテキストファイルに出力をエクスポートするためにはどのような変更を行うべきか、本当に助けていただきありがとうございます。

はのが主なコードがあるとしましょう:

// Driver program 
int main() 
{ 
    // Create a graph given in the above diagram 
    Graph g(4); 
    g.addEdge(0, 1); 
    g.addEdge(0, 2); 
    g.addEdge(0, 3); 
    g.addEdge(2, 0); 
    g.addEdge(2, 1); 
    g.addEdge(1, 3); 
    int s = 2, d = 3; 
    g.printAllPaths(s, d); 

    return 0; 
} 

我々はすでにg.printAllPathsg.addEdgeを定義しました。

ofstream outFile; 
outFile.open("sample.txt"); 
outFile <<g.printAllPaths(s, d)<<endl; 
outFile.close(); 

が、これは動作しません:私が何をしようとしています何

は、テキストファイルを作成し、そこに出力を書き込みます。

+2

「このdoesnの:

std::ostream& printAllPaths(std::ostream& outfile, int s, int d) { // Output to the stream, // Example: outfile << s << ", " << d; return outfile; } 

代替がoperator<<でないチェーンの機能をしています'仕事'は有用な問題の説明ではありません。 "こんにちは、トムの自動車修理?私の車は動作しません、それは修正することはできますか?" –

+0

Graph.printAllPathsは何を返しますか? const char&または何ですか? – RamblinRose

+1

'Graph :: printAllPaths'のコードであなたの投稿を編集してください。 –

答えて

0

あなたはそれがストリームを返すようにする必要があります、operator<<で機能を使用するには:

void Graph::printAllPaths(std::stream& outfile, int s, int d) 
{ 
    outfile << s << ", " << d; 
} 

// Usage 
std::ofstream outfile("sample.txt"); 
if (outfile) 
{ 
    g.printAllPaths(outfile, s, d); 
    outfile << "endl"; 
    outfile.close(); 
}