栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

C++从文件中读取数据

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

C++从文件中读取数据

13.1 using cin
1 You must include the iostream header file.
2 The iostream header file defines an istream class for handling input.
3 The iostream header file declares an istream variable, or object, called cin.
4 You must account for the std namespace; for example, you can use the using directive
or the std:: prefix for elements such as cin.
5 You can use cin with the >> operator to read a variety of data types.
6 You can use cin with the get() method to read individual characters and with the
getline() method to read a line of characters at a time.
7 You can use cin with methods such as eof() and fail() to monitor the success of
an input attempt.
8 The object cin itself, when used as a test condition, is converted to the Boolean
value true if the last read attempt succeeded and to false otherwise.

13.2 using ifstream
1 You must include the fstream header file.
2 The fstream header file defines an ifstream class for handling input.
3 You need to declare one or more ifstream 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 ifstream.
5 You need to associate a specific ifstream 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 ifstream object with the >> operator to read a variety of data types.
8 You can use an ifstream object with the get() method to read individual characters
and with the getline() method to read a line of characters at a time.
9 You can use an ifstream object with methods such as eof() and fail() to monitor
the success of an input attempt.
10 An ifstream object itself, when used as a test condition, is converted to the
Boolean value true if the last read attempt succeeded and to false otherwise.

13.3 实例

code:

// File name:           sumafile
// Last modified Date:  2021年11月21日16点51分
// Last Version:        V1.0
// Descriptions:        从文件中读取数据

// sumafile.cpp -- functions with an array argument
#include 
#include  // file I/O support
#include  // support for exit()
const int SIZE = 60;
int main()
{
	using namespace std;
	char filename[SIZE];
	ifstream inFile; // object for handling file input
	cout << "Enter name of data file: ";
	cin.getline(filename, SIZE);
	inFile.open(filename); // associate inFile with a file

	if (!inFile.is_open()) // failed to open file
	{
		cout << "Could not open the file " << filename << endl;
		cout << "Program terminating.n";
		exit(EXIT_FAILURE);
	}
	double value;
	double sum = 0.0;
	int count = 0; // number of items read
	inFile >> value; // get first value
	while (inFile.good()) // while input good and not at EOF  在倒数第二个数据读取到时就会为假,所以最后一个数据没有读取到
	{
		++count; // one more item read
		sum += value; // calculate running total
		inFile >> value; // get next value
	}
	//因此在这加两句读取最后一个数据
	++count; // one more item read
	sum += value; // calculate running total

	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(); // finished with the file
	return 0;
}

运行结果:

Enter name of data file: scores.txt
End of file reached.
Items read: 12
Sum: 204.5
Average: 17.0417

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

文件内容为:

18 19 18.5 13.5 14 16 19.5 20 18 12 18.5
17.5

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/691568.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号