2017-10-28 5 views
0

forループで以下の関数を呼び出します。各反復で、端末にいくつかの数字が表示されます。数字は距離で区切られています。私はプログラムが完全に実行された後、txtファイルに数字を書きたいので、txtファイルのすべての数字にアクセスできます。これは私の機能です:forループで呼び出す関数の出力をtxtに保存する(C++)

void printComponents(Components const& cc) 
    { 
    auto id = 0; 
    for (auto& c : cc) { 
     int counter=0; 
     for (auto v : c) 
      { 
      counter ++; 
      } 
     if (counter >1) 
      { 
      std::cout << " " << counter;  
      } 
         } 
    } 

数字をtxtファイルで順番に保存できますか?

+0

はあなたでした私が投票できる完全な回答を提供しますか? @Ron –

+0

別のパラメータ 'std :: ostream&out'を追加し、' std :: cout'を 'out'に置き換えてください。 – user0042

+0

@ user0042 void printComponents(コンポーネントconst&cc、std :: ostream&out)を意味しますか? \t { –

答えて

0

あなたは、あなたの関数

void printComponents(Components const& cc, std::ostream& out) { 
    // ... 
} 

に別のパラメータstd::ostream& outを追加し、out

out << " " << counter; 

std::coutを置き換えるテキストファイルを開いて、あなたの関数に渡し

std::ofstream file("MyFile.txt"); 
for(...) { 
    printComponents(components,file); 
} 
+0

最終行のファイルとは何ですか? –

+0

@Monaphys前の行で構築された 'std :: ofstream'変数です。 – user0042

+0

変数 'std :: ofstream file'に初期化子がありますが、不完全な型があります。std :: ofstream file( "MyFile.txt"); –

関連する問題