3 回答

TA貢獻(xiàn)1993條經(jīng)驗(yàn) 獲得超6個(gè)贊
將內(nèi)容輸出到文本中要用ofstream這個(gè)類來實(shí)現(xiàn)。具體步驟如下。
ofstream mycout("temp.txt");//先定義一個(gè)ofstream類對(duì)象mycout,括號(hào)里面的"temp.txt"是我們用來保存輸出數(shù)據(jù)的txt文件名。這里要注意的是我們的"temp.txt"用的是相對(duì)路徑,你也可以寫絕對(duì)路徑。
mycout<<"hello"<<endl;//這樣就把"hello"輸出到temp.txt文件中了
mycout.close();//最后要記得關(guān)閉打開的文件(這里打開的就是temp.txt文件)
現(xiàn)在給你提供一個(gè)完整的程序來實(shí)現(xiàn)你說的將輸入的內(nèi)容輸出到文件
#include <iostream>
#inlcude <fstream>//ofstream類的頭文件
using namespace std;
int main()
{
int n;
cin>>n;
ofstream mycout("temp.txt");
mycout<<n<<endl;
mycout.close();
return 0;
}

TA貢獻(xiàn)1963條經(jīng)驗(yàn) 獲得超6個(gè)贊
1 2 3 4 5 6 7 8 9 10 11 | #include<iostream> #include<fstream> using namespace std; int main() { ofstream out("e:/file.txt"); //保存到e盤的文件file.txt中 char buffer[100]; gets(buffer); out<<buffer; return 0; } |
- 3 回答
- 0 關(guān)注
- 593 瀏覽
添加回答
舉報(bào)