C++中的文件输入输出,读写
#include<iostream>
#include<fstream> //文件读写的库文件
int main(){
using namespace std;
char automobile[50];
int year;
double a_price;
double d_price;
ofstream outFile;
outFile.open("carinfo.txt");
cout << "enter teh make and modle od automobile :";
cin.getline(automobile , 50);
cout << " enter the model yeay : ";
cin >> year ;
cout << "enter the original asking price :" ;
cin >>a_price ;
d_price = 0.913*a_price ;
//在屏幕上输出
cout << fixed ;
cout.precision(2);
cout.setf(ios_base::showpoint);
cout << " make and modle : " <<automobile <<endl;
cout << "year :" << year <<endl;
cout << "was asking $ "<< a_price <<endl;
cout << "now asking $ "<< d_price <<endl;
//在项目目录下生成文件<span style="font-family: Arial, Helvetica, sans-serif;">carinfo.txt ,将输入输出写入文件</span>
outFile << fixed ;
outFile.precision(2);
outFile.setf(ios_base::showpoint);
outFile << " make and modle : " <<automobile <<endl;
outFile << "year :" << year <<endl;
outFile << "was asking $ "<< a_price <<endl;
outFile << "now asking $ "<< d_price <<endl;
outFile.close();
return 0;
}