c++将数据输出到文件

c++将数据输出到文件,第1张

c++将数据输出到文件 1.1 using cout
1. You must include the iostream header file.
2. The iostream header file defines an ostream class for handling output.
3. The iostream header file declares an ostream variable, or object, called cout.
4. You must account for the std namespace; for example, you can use the using directive
or the std:: prefix for elements such as cout and endl.
5. You can use cout with the << operator to read a variety of data types.
1.2 using ofstream
1 You must include the fstream header file.
2 The fstream header file defines an ofstream class for handling output.
3 You need to declare one or more ofstream variables, or objects, which you can
name as you please, as long as you respect the usual naming conventions.
4 You must account for the std namespace; for example, you can use the using directive
or the std:: prefix for elements such as ofstream.
5 You need to associate a specific ofstream object with a specific file; one way to do
so is to use the open() method.
6 When you’re finished with a file, you should use the close() method to close the file.
7 You can use an ofstream object with the << operator to output a variety of data types.
1.3 实例

code:

// File name:           outfile
// Last modified Date:  2021年11月21日16点40分
// Last Version:        V1.0
// Descriptions:        输出数据到文件的简单实例

// outfile.cpp -- writing to a file
#include 
#include  // for file I/O
int main()
{
	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 information on screen 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 do exact same things using 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(); // done with file  用完文件一定要把它关上
	return 0;
}

运行结果:

Enter the make and model of automobile: Superboly
Enter the model year: 2019
Enter the original asking price: 9999
Make and model: Superboly
Year: 2019
Was asking $9999.00
Now asking $9129.09

D:PrjC++C++_LearningoutfileDebugoutfile.exe (进程 17460)已退出,代码为 0。
要在调试停止时自动关闭控制台,请启用“工具”->“选项”->“调试”->“调试停止时自动关闭控制台”。
按任意键关闭此窗口. . .

并且生成了这样的文件:

文件中的内容为:

Make and model: Superboly
Year: 2019
Was asking 99.00
Now asking 29.09

 

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5691985.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-17
下一篇 2022-12-17

发表评论

登录后才能评论

评论列表(0条)

保存