C++實(shí)現(xiàn)將輸入的內(nèi)容輸出到文本文件
C++將輸入的內(nèi)容輸出到文本文件
將內(nèi)容輸出到文本中要用ofstream這個(gè)類來(lái)實(shí)現(xiàn)。
具體步驟如下:
ofstream mycout(“temp.txt”);//先定義一個(gè)ofstream類對(duì)象mycout,括號(hào)里面的"temp.txt"是我們用來(lái)保存輸出數(shù)據(jù)的txt文件名。這里要注意的是我們的"temp.txt"用的是相對(duì)路徑,你也可以寫絕對(duì)路徑。 mycout<<“hello”<<endl;//這樣就把"hello"輸出到temp.txt文件中了 mycout.close();//最后要記得關(guān)閉打開(kāi)的文件(這里打開(kāi)的就是temp.txt文件)
現(xiàn)在給你提供一個(gè)完整的程序來(lái)實(shí)現(xiàn)你說(shuō)的將輸入的內(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++簡(jiǎn)單文件輸入/輸出
1、寫入到文本文件
- 包含頭文件fstream
- 頭文件fstream中定義了一個(gè)用于處理輸出的ofstream類
- 需要聲明一個(gè)或多個(gè)ofstream變量(對(duì)象),
- 將ofstream對(duì)象與文件關(guān)聯(lián)起來(lái)。常用的方法是open()
- 使用完文件后,應(yīng)使用方法close()將其關(guān)閉
- 可結(jié)合ofstream對(duì)象和運(yùn)算符<<來(lái)輸出各種類型的數(shù)據(jù)
當(dāng)創(chuàng)建好一個(gè)ofstream對(duì)象后,便可以像使用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");//打開(kāi)文件,或者創(chuàng)建文件,總之就是和一個(gè)文件關(guān)聯(lián)
//默認(rèn)情況下,open()將首先截?cái)嘣撐募?,即將其長(zhǎng)度截短到零——丟棄原有內(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)建文件輸入對(duì)象,并打開(kāi)
ifstream inFile;
inFile.open("carimfo.txt");
//判斷文件是否打開(kāi)
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++在讀取文件時(shí)將這兩個(gè)字符轉(zhuǎn)換為換行符,并在寫入文件時(shí)執(zhí)行相反的轉(zhuǎn)換。
有些文本編輯器,不會(huì)在4自動(dòng)在最后一行末尾加上換行符。
因此,如果讀者使用的是這種編輯器,請(qǐng)?jiān)谳斎胱詈蟮奈谋局蟀聪禄剀囨I,再保存文件,否則最后一個(gè)數(shù)據(jù)將出現(xiàn)問(wèn)題。
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
C++實(shí)現(xiàn)一個(gè)簡(jiǎn)單消息隊(duì)列的示例詳解
消息隊(duì)列在多線程的場(chǎng)景有時(shí)會(huì)用到,尤其是線程通信跨線程調(diào)用的時(shí)候,就可以使用消息隊(duì)列進(jìn)行通信。本文將利用C++實(shí)現(xiàn)一個(gè)簡(jiǎn)單的消息隊(duì)列,感興趣的可以了解一下2022-12-12
C語(yǔ)言指針之必須要掌握的指針基礎(chǔ)知識(shí)
這篇文章主要介紹了C語(yǔ)言指針必須要掌握的基礎(chǔ)知識(shí),文中實(shí)例講解的很清晰,有不太懂的同學(xué)可以研究下,希望能夠給你帶來(lái)幫助2021-09-09
C++ 中dynamic_cast<>的使用方法小結(jié)
將一個(gè)基類對(duì)象指針(或引用)cast到繼承類指針,dynamic_cast會(huì)根據(jù)基類指針是否真正指向繼承類指針來(lái)做相應(yīng)處理2013-03-03
C++?opencv圖像處理使用cvtColor實(shí)現(xiàn)顏色轉(zhuǎn)換
這篇文章主要為大家介紹了C++?opencv圖像處理cvtColor實(shí)現(xiàn)顏色轉(zhuǎn)換的實(shí)現(xiàn)示例代碼,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-05-05
詳解C語(yǔ)言中二級(jí)指針與鏈表的應(yīng)用
對(duì)于初學(xué)者而言,有很多地方肯定是費(fèi)解的。比如函數(shù)的參數(shù)列表的多樣化,動(dòng)態(tài)分配內(nèi)存空間函數(shù)malloc等,其實(shí)這些知識(shí)和指針聯(lián)系緊密,尤其是二級(jí)指針,快跟隨小編來(lái)學(xué)習(xí)一下吧2022-07-07

