C++實現(xiàn)將輸入的內(nèi)容輸出到文本文件
C++將輸入的內(nèi)容輸出到文本文件
將內(nèi)容輸出到文本中要用ofstream這個類來實現(xiàn)。
具體步驟如下:
ofstream mycout(“temp.txt”);//先定義一個ofstream類對象mycout,括號里面的"temp.txt"是我們用來保存輸出數(shù)據(jù)的txt文件名。這里要注意的是我們的"temp.txt"用的是相對路徑,你也可以寫絕對路徑。 mycout<<“hello”<<endl;//這樣就把"hello"輸出到temp.txt文件中了 mycout.close();//最后要記得關(guān)閉打開的文件(這里打開的就是temp.txt文件)
現(xiàn)在給你提供一個完整的程序來實現(xiàn)你說的將輸入的內(nèi)容輸出到文件
#include <iostream>
#include <fstream>//ofstream類的頭文件
using namespace std;
int main()
{
int n;
cin>>n;
ofstream mycout("temp.txt");
mycout<<n<<endl;
mycout.close();
return 0;
}C++簡單文件輸入/輸出
1、寫入到文本文件
- 包含頭文件fstream
- 頭文件fstream中定義了一個用于處理輸出的ofstream類
- 需要聲明一個或多個ofstream變量(對象),
- 將ofstream對象與文件關(guān)聯(lián)起來。常用的方法是open()
- 使用完文件后,應(yīng)使用方法close()將其關(guān)閉
- 可結(jié)合ofstream對象和運算符<<來輸出各種類型的數(shù)據(jù)
當(dāng)創(chuàng)建好一個ofstream對象后,便可以像使用cout一樣使用它。
#include<iostream>
#include<fstream>
using namespace std;
int main()
{
char automobile[50];
int year;
double a_price;
double b_price;
ofstream outFile;
outFile.open("carimfo.txt");//打開文件,或者創(chuàng)建文件,總之就是和一個文件關(guān)聯(lián)
//默認(rèn)情況下,open()將首先截斷該文件,即將其長度截短到零——丟棄原有內(nèi)容,將新的輸出加入到文件。
cout << "Enter the make and model of automobile:";
cin.getline(automobile, 50);
cout << "Enter the model year:";
cin >> year;
cout << "Enter the original asking price:";
cin >> a_price;
b_price = 0.913 * a_price;
//寫入文件
outFile << "Make and model: " << automobile << endl;
outFile << "Year: " << year << endl;
outFile << "Was asking $" << a_price << endl;
outFile << "Now asking $" << b_price << endl;
outFile.close();//關(guān)閉文件
system("pause");
return 0;
}2、讀取文本文件
其操作和輸出相似
#include<iostream>
#include<fstream>
using namespace std;
int main()
{
//創(chuàng)建文件輸入對象,并打開
ifstream inFile;
inFile.open("carimfo.txt");
//判斷文件是否打開
if (!inFile.is_open())
{
cout << "Could not open the file carimfo.txt" << endl;
cout << "Program terminating.\n";
//函數(shù)exit()終止程序 EXIT_FAILURE用于同操作系統(tǒng)通信的參數(shù)值
exit(EXIT_FAILURE);
}
double value;
double sum = 0.0;
int count = 0;
while (inFile>>value)
{
//cout<<value<<endl;
count++;
sum += value;
inFile >> value;
}
if (inFile.eof())
cout << "End of file reached.\n";
else if (inFile.fail())
cout << "Input terminated by data mismatch.\n";
else
cout << "Input terminated for unknown reason.\n";
if (count == 0)
cout << "NO data processed.\n";
else{
cout << "Items read: " << count << endl;
cout << "Sum: " << sum << endl;
cout << "Average: " << sum / count << endl;
}
inFile.close();
system("pause");
return 0;
}注意:
Windows文本文件的每行都以回車字符和換行符結(jié)尾;通常情況下,C++在讀取文件時將這兩個字符轉(zhuǎn)換為換行符,并在寫入文件時執(zhí)行相反的轉(zhuǎn)換。
有些文本編輯器,不會在4自動在最后一行末尾加上換行符。
因此,如果讀者使用的是這種編輯器,請在輸入最后的文本之后按下回車鍵,再保存文件,否則最后一個數(shù)據(jù)將出現(xiàn)問題。
總結(jié)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Matlab實現(xiàn)繪制有氣泡感的網(wǎng)絡(luò)圖
這篇文章主要介紹了如何利用Matlab實現(xiàn)繪制有氣泡感的網(wǎng)絡(luò)圖,文中的示例代碼講解詳細(xì),對我們學(xué)習(xí)Matlab有一定的幫助,需要的可以參考一下2023-02-02
MFC串口通信發(fā)送16進(jìn)制數(shù)據(jù)的方法
這篇文章主要為大家詳細(xì)介紹了MFC串口通信發(fā)送16進(jìn)制數(shù)據(jù),具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-01-01
C++?反匯編之關(guān)于Switch語句的優(yōu)化措施
這篇文章主要介紹了C++?反匯編之關(guān)于Switch語句的優(yōu)化措施,利用三種優(yōu)化來降低樹高度,誰的效率高就優(yōu)先使用誰,三種優(yōu)化都無法匹配才會使用判定樹,具體內(nèi)容詳情跟隨小編一起看看吧2022-01-01
C++實現(xiàn)LeetCode(25.每k個一組翻轉(zhuǎn)鏈表)
這篇文章主要介紹了C++實現(xiàn)LeetCode(25.每k個一組翻轉(zhuǎn)鏈表),本篇文章通過簡要的案例,講解了該項技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-07-07

