![](/img/trans.png)
[英]How can I clean this program efficiently and still detect when the file input is not a letter?
[英]How can i capture the input and output to a file when running a program?
我正在用c ++工作,我需要一些方法來捕獲控制台的輸入和輸出,就像我直接通過終端一樣。
所以,假設我有一個可執行的test.exe和一個輸入文件input.txt,我想將輸入和輸出的組合保存到console.out我需要做什么終端命令?
我在linux命令方面不是很好,即使我盡力去谷歌,所以如果你知道你的幫助將不勝感激!
例如,如果輸入文件中包含:
show
ignore
hide
並且,在使用此輸入運行我的程序后,輸出文件包含在其中:
Enter Command:
/****SHOWING DATA!****/
Enter Command:
/****IGNORING DATA!***/
Enter Command:
/***HIDING DATA!***/
我想要一個看起來像這樣的文件:
Enter Command: show
/****SHOWING DATA!****/
Enter Command:ignore
/****IGNORING DATA!***/
Enter Command:hide
/***HIDING DATA!***/
因此,如果我在沒有任何重定向的情況下運行它,那么它將捕獲我在終端中看到的內容。
您可以嘗試重定向> console.out
來捕獲輸出。 script命令將捕獲整個會話。
讓您的程序除了輸出到屏幕外,還輸出到文件中。 由於您正在解析用戶輸入的命令,您還應該從變量中訪問它們,也可以輸出它們。
//PSEUDO-CODE!
std::ofstream fout;
fout.open("output.txt");
std::cout << "Enter Command: ";
fout << "Enter Command: ";
std::cin >> cmd;
fout << cmd << '\n';
//..Output results to file.
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.