C++ 读取文本文件(文件输入/输出)

目录

一、文件的输出

二、读取文本文件

三、编程实例


一、文件的输出

        对于文件的输入C++使用类似于cout的东西

        1.必须包含头文件iostream、fstream

        2.需要声明一个或者多个ofstream变量,并为他命名

        3.使用完文件后 要用close()将其关闭

        4.可以结合ofstream对象和运算符<<来输出各种类型的数据

命名方式

ofstream outFile;

将对象与特定的文件关联起来

outFile.open("giantfox.txt");

下面是将所输出的内容写到文件giantfox.txt 文件下

#include;
using namespace std;
#include
#include
int other() {
	char automobile[50];
	int year;
	double a_price;
	double d_price;
	ofstream outFile;
	outFile.open("giantfox.txt");
	cout << "ENter athe make ";
	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;
	outFile << fixed;
	outFile.precision(2);
	outFile.setf(ios_base::showbase);
	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;

}

C++ 读取文本文件(文件输入/输出)_第1张图片

文件输入成功 

二、读取文本文件

要求:

        

        1.必须包含头文件iostream、fstream

        2.需要声明一个或者多个ifstream变量,并为他命名

        3.使用完文件后 要用close()将其关闭

        4.可以结合ifstream对象和运算符>>来读取各种类型的数据

        5.可以使用cin和get()来读取一个字符 使用cin getline()读取一行字符

        6.可以结合使用cin和eof()、fail()方法来判断输入是否成功

声明对象

ifstream inFile;

将对象和实例结合

inFile.open("giant fox");

检查一个文件是否打开

inFile.open("giant fox.txt");

if (!inFile.is_open())

{

        exit(EXIT_FAILTURE);

}

如果文件成功打开,方法is_open将返回true;因此如果文件没有被打开此条件语句的返回值将为true。函数exit的原型是再头文件cstdlib定义的,在该头文件中,还定义了一个用于同操作系统通信的参数值EXIT_FAILTURE.函数exit()终止程序

检查文件是否读取到文本末尾

if (inFile.eof())
    {
        cout << "End of the file reached\n";

    }

当文件打开失败

    else if (!inFile.fail()) {
        cout << "Input terminated by data mismatch. \n";
    }
    else
        cout << "Unknown reason. \n";

统计文件字符小程序

#include;
#include
using namespace std;
#include
#include
#include

int other() {
	ifstream inFile;
	char fileame[50];
	cout << "Enter name of data file: ";
	cin.getline(fileame, 50);
	inFile.open(fileame);
	if (!inFile.is_open()) {
		cout << "Could not open the file ";
		exit(EXIT_FAILURE);
	}
	char value;
	int count = 0;
	inFile >> value;
	while (inFile.good())
	{
		count++;
		inFile >> value;
	}
	if (inFile.eof())
	{
		cout << "End of the file reached\n";

	}
	else if (!inFile.fail()) {
		cout << "Input terminated by data mismatch. \n";
	}
	else
		cout << "Unknown reason. \n";
	if (count == 0)
		cout << "No database";
	else
	{
		cout << "该文件有: "<

三、编程实例

编写一个程序,记录捐助给"维护合法权利团体"的资金。该程序要求用户输入捐赠者数目,然后要求用户输入每一个捐献者的姓名和款项。这些信息被存储在一个动态分配的结构数组中.每个数据结构有两个成员:用来存储姓名的字符串数组(或string对象)和用来存储款项的double成员。读取所有的数据后,程序将显示所有捐款超过10000的捐款着姓名及其捐款数额。该列表前应包含一个标题,指出下面的捐款者是重要捐款人(Grand Patrons)。然后,程序将列出其他的捐款者,该列表要以Partons开头。如果某种类别没有捐款者,则程序将打印单词"none"。该程序只显示这两种类别,而不进行排序。

#include;
using namespace std;
#include
struct donor {
	string name;
	double money;
};
void display(donor donors[], int number, bool isGrand) {
	int count = 0;
	if (isGrand) {
		for (int i = 0; i < number; i++) {
			if (donors[i].money > 10000) {
				count++;
				
				cout << donors[i].name << endl;
				cout << donors[i].money << endl;
			}
		}
	}
	else
	{
		for (int i = 0; i < number; i++) {
			if (donors[i].money <= 10000) {
				count++;
				cout << donors[i].name << endl;
				cout << donors[i].money << endl;
			}
		}
	}
	if (number == 0)
	{
		cout << "None" << endl;
	}
}
int other() {

	cout << "Enter the donors of number: ";
	int number;
	cin >> number;
	donor* donors = new donor[number];
	for (int i = 0; i < number; i++) {
		cout << "Enter the donor #"<> donors[i].name;
		cout << "Enter the doctnation #"<> donors[i].money;

	}
	cout << "***A****" << endl;
	display(donors, number, true);
	cout << "***B****" << endl;
	display(donors, number, false);
	return 0;
}

完成上述编程后,从文件中读取所需的信息。该文件的第一项应为捐款人数,余下的内容应为对的行。在每一对中,第一行为捐款人姓名,第二行为捐款数额。即该文件类似于下面:

4
Sam Stone
2000
Freida Flass
100500
Tammy Tubbs
5000
Rich Raptor
55000
 

#include;
using namespace std;
#include
#include
#include
struct donor {
	char name[20];
	double money;
};
void display(donor donors[], int number, bool isGrand) {
	int count = 0;
	if (isGrand) {
		for (int i = 0; i < number; i++) {
			if (donors[i].money > 10000) {
				count++;

				cout << donors[i].name << endl;
				cout << donors[i].money << endl;
			}
		}
	}
	else
	{
		for (int i = 0; i < number; i++) {
			if (donors[i].money <= 10000) {
				count++;
				cout << donors[i].name << endl;
				cout << donors[i].money << endl;
			}
		}
	}
	if (number == 0)
	{
		cout << "None" << endl;
	}
}
int other() {
	ifstream inFile;
	inFile.open("giantfox.txt");
	char value;
	if (!inFile.is_open()) {
		cout << "Could not open the file: ";
		exit(EXIT_FAILURE);

	}
	int number = 0;
	inFile >> number;
	char ch[30];
	inFile.getline(ch, 1);
	donor* donors = new donor[number];
	for (int i = 0; i < number; i++) {
		inFile.getline(donors[i].name, 30);
		inFile >> donors[i].money;
		inFile.getline(ch, 1);
	}
	cout << "******Grand Partons******" << endl;
	display(donors, number, true);
	cout << "******Partons******" << endl;
	display(donors, number, false);
	delete[] donors;
	return 0;
}

附注:在读取文本的时候 结构体的字符串类型要发生改变,不然getline函数用不了

你可能感兴趣的:(C++,c++,开发语言)