2017-09-08 2 views
1

私はデータを表示するためにコマンドラインを使用するスクリプトを作成しようとしています。コマンドラインに出力されたすべてのものを別々のファイルに記録する必要があります。C++でコマンドラインを記録する

私はfstream、iostream、およびstd名前空間を使用しています。私は、コマンドラインCmdExample.exeとそれをtxtファイルに書き込むためのすべてを参照する方法を知る必要があります。

例:

#include <iostream> 
#include <fstream> 
using namespace std; 

int main() 
{ 
    cout << "This is some text I want to reference when the program ends" << endl << "and write to a txt file."; 

    return 0; 
} 
+2

通常、 'CmdExample.exe> output.txt'を実行します。 – nwp

+1

おそらく、https://stackoverflow.com/questions/24413744/using-operator-to-write-to-both-a-file-and-coutのようなものを探しています。 –

答えて

2

あなたは、出力のリダイレクトを使用することができます。すなわち:

./a.out > my_output.txt 

これは新しいファイルに端末への出力、my_output.txt(またはファイル内に既に何かを上書き)してきたでしょうすべてを置くだろう。あなたにもcerrによって物事の出力が必要な場合

、あなたはそれがために変更することができます。

./a.out 2>&1 > my_output.txt #push stderr -> stdout, then stdout -> my_output.txt 

同様に、あなたもscriptを使用することができます。スクリプトを使用するには、次のようにします:

script my_output.txt #start the script to my_output.txt 
./a.out #run program 
exit #end script 
+0

私はおそらくLinux環境でコーディングを開始すべきだと思います。 –

+1

@Jones Crimsonには、うまく動作するWindows用のbashエミュレータがあります。 – scohe001

関連する問題