[英]How to view the output of c++ program which compiled on terminal
#include<iostream>
using namespace std;
int main()
{
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
int a,b;
cin >> a >> b;
cout << a+b;
return 0;
}
當我從終端機(Mac)進行編譯時,它不是從input.txt讀取的意思,也不寫入output.txt。 問題是什么?
您忘記優雅地關閉文件
fclose (stdin);
fclose (stdout);
注意:fclose()函數刷新stdout指向的流(使用fflush寫入任何緩沖的輸出數據並關閉基礎文件描述符。
@Chingy,我看到了您的截圖。 請查看此文檔以獲取g ++的基本介紹。 請注意,程序的編譯和執行是不同的實例。
第一步是編譯,您可以按照屏幕快照正確地進行編譯。
1)g ++ 1.cpp。
此步驟將創建一個可執行文件(a.out),您需要單獨運行該可執行文件才能從程序中獲得所需的行為。
請執行以下程序:
2)./a.out
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.