ifstream& operator>>函数无法获取空行和peek函数的用法

本文探讨了使用C++标准库中的ifstream与运算符重载进行文件读取时遇到的问题,特别是当文件包含空行时istream提取运算符的行为。文中提供了一个简单的示例程序来展示如何检查文件是否为空,并尝试读取文件内容。
ifstream& operator>>函数无法获取空行

#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
	string str;
	ifstream fin("c:\\1.txt"/*, ios::binary*/);
	if(!fin.is_open())
	{
		cout<<"打开失败\r\n";
		return 0;
	}

	if (fin.peek() == EOF)
	{
		cout << "file is empty."<<endl;
		return 0;
	}

	int count=0;
	while (!fin.eof())
	{
		fin >> str;
		cout << str<<endl;
		count++;
	}    
	system("pause");
	return 0;
}


 

 

peek函数才能够做到防止空文件的出现

 

#include <iostream> #include <iomanip> #include <fstream> using namespace std; // 定义学生结构体 class Student { int id; string name; int chinese; int math; int english; int total; public: bool operator>(Student& s) { return total > s.total; }// 按总成绩从高到低排序要用到 void calculateTotal() { total = chinese + math + english; } friend ostream& operator<<(ostream& out, Student& stu); friend istream& operator>>(istream& in, Student& stu); friend ifstream&amp; operator>>(ifstream&amp; input, Student& stu); friend void sort(Student* stus, int n); }; /***********begin************/ ifstream&amp; operator>>(ifstream&amp; input, Student& stu) { input >> stu.id >> stu.name >> stu.chinese >> stu.math >> stu.english; return input; } istream& operator>>(istream& in, Student& stu) { in >> stu.id>>stu.name>>stu.chinese>>stu.math>>stu.english; return in; } ostream& operator<<(ostream& out, Student& stu) { out << stu.id <<' '<< stu.name << ' ' << stu.chinese << ' ' << stu.math << ' ' << stu.english <<' '<<stu.total<< endl; return out; } void sort(Student* stus, int n) { for (int a = 0;a < n;a++) { for (int b = a + 1;b < n;b++) { if (stus[a].total < stus[b].total) { Student temp = stus[a]; stus[a] = stus[b]; stus[b] = temp; } } } } /***********end************/ int main() { // 1. 输入学生信息并写入文件 result.dat ofstream outFile("result.txt"); if (!outFile) { cerr << "无法打开文件 result.txt" << endl; return 1; } int n; cin >> n; /***********创建动态数组stus************/ Student* stus = new Student[n]; for (int i = 0; i < n; i++) { cin >> stus[i]; stus[i].calculateTotal(); outFile << stus[i]; /***********保存学生信息到文件result.txt************/ } outFile.close(); // 2. 从 result.txt 中读取数据并排序 ifstream inFile("result.txt"); if (!inFile) { cerr << "无法打开文件 result.txt" << endl; return 1; } int i = 0; while ( inFile>>stus[i] /***********从文件result.txt读取学生信息并存入stus[i]************/) { i++; } inFile.close(); /***********调用前面定义的sort函数对数组stus的信息按总分排序************/ sort(stus,n); // 3. 将排序后的数据写入文件 sort.txt,并输出到屏幕 ofstream sortedFile("sort.txt"); if (!sortedFile) { cerr << "无法打开文件 sort.txt" << endl; return 1; } for (i = 0; i < n;i++) { sortedFile << stus[i]; cout << stus[i]; } sortedFile.close(); delete[]stus; return 0; }
03-30
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值