// Chapter6_outfile.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//
#include <iostream>
#include <fstream>
int main()
{
std::cout << "Hello World!\n";
using namespace std;
char automobile[50];
int year;
double a_price;
double d_price;
ofstream outFile;//create object for output
outFile.open("carinfo.txt");//associate with a file
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;
d_price = 0.913 * a_price;
//display with cout
cout << fixed;
cout.precision(2);
cout.setf(ios_base::showpoint);
cout << "Make and model:" << automobile << endl;
cout << "Year:" << year << endl;
cout << "Was asking $:" << a_price << endl;
cout << "Now asking $:" << d_price << endl;
//now use the outFile instead of cout
outFile << fixed;
outFile.precision(2);
outFile.setf(ios_base::showpoint);
outFile << "Make and model:" << automobile << endl;
outFile << "Year:" << year << endl;
outFile << "Was asking $:" << a_price << endl;
outFile << "Now asking $:" << d_price << endl;
outFile.close();
return 0;
}
// 运行程序: Ctrl + F5 或调试 >“开始执行(不调试)”菜单
// 调试程序: F5 或调试 >“开始调试”菜单
// 入门使用技巧:
// 1. 使用解决方案资源管理器窗口添加/管理文件
// 2. 使用团队资源管理器窗口连接到源代码管理
// 3. 使用输出窗口查看生成输出和其他消息
// 4. 使用错误列表窗口查看错误
// 5. 转到“项目”>“添加新项”以创建新的代码文件,或转到“项目”>“添加现有项”以将现有代码文件添加到项目
// 6. 将来,若要再次打开此项目,请转到“文件”>“打开”>“项目”并选择 .sln 文件
C++PrimerPlus(第6版)中文版:Chapter6_outfile
最新推荐文章于 2024-01-14 21:32:10 发布
本文档展示了如何使用C++编写一个简单的程序,从用户输入获取汽车型号、年份和价格,然后将其写入到'carinfo.txt'文件中,同时展示了使用`ofstream`操作文件的基本流程。
631

被折叠的 条评论
为什么被折叠?



