C++文件操作,及相关例题

文章展示了C++中如何进行文本文件和二进制文件的读写操作,包括ofstream和ifstream类的使用,以及如何处理字符和结构体数据。还提供了从键盘读取数据并保存到磁盘,以及从磁盘读取数据并进行处理的示例。
摘要由CSDN通过智能技术生成

1.文本文件--写操作

#include<iostream>
using namespace std;
#include<fstream>
int main()
{
	//1.包含头文件
	//2.创建流对象
	ofstream ofs;

	//3.打开文件并判断是否打开
	ofs.open("t1.txt",ios::out);
	if (!ofs.is_open())
	{
		cout << "文件打开失败!" << endl;
	}

	//4.操作文件
	ofs << "张三" << endl;
	ofs << "男" << endl;
	ofs << 18 << endl;

	//5.关闭文件
	ofs.close();
	return 0;
}

2.文本文件--写操作

#include<iostream>
using namespace std;
#include<fstream>
#include<string>
int main()
{
	//1.包含头文件
	
	//2.创建流对象
	ifstream ifs;
	//3.打开文件并判断是否打开成功
	ifs.open("t1.txt",ios::in);
	if (!ifs.is_open())
	{
		cout << "file opening failure! " << endl;
	}
	//4.操作文件
	/*char ch[100];
	while (ifs>>ch)
	{
		cout << ch<<endl;
	}*/
	string buf;
	while (getline(ifs,buf))
	{
		cout << buf << endl;
	}
	//5.关闭文件
	return 0;
}

3.二进制文件--写操作

#include<iostream>
using namespace std;
#include<fstream>
class Student
{
public:
	char Name[20];
	char Gender;
	int Age;
};
int main()
{
	ofstream ofs;//向磁盘文件输出
	ofs.open("s2.txt",ios::out|ios::binary);
	if (!ofs.is_open())
	{
		cout << "file opening failure" << endl;
	}
	//写文件
	Student s1 = {"张三",'男',18};
	ofs.write((const char *)&s1,sizeof(Student));
	//关闭文件
	ofs.close();
	return 0;
}

4.二进制文件--读操作

#include<iostream>
using namespace std;
#include<fstream>
class Student
{
public:
	char Name[20];
	char Gender;
	int Age;
};
int main()
{
	ifstream ifs;//从磁盘文件输入
	ifs.open("s2.txt",ios::in|ios::binary);
	if (!ifs.is_open())
	{
		cout << "file opening failure" << endl;
	}
	Student s1;
	ifs.read((char*)&s1,sizeof(Student));
	cout << s1.Name << s1.Gender << s1.Age << endl;
	ifs.close();
	return 0;
}

5.案例1

//有一个整型数组,含有10个元素,从键盘上读入这10个整数,给数组,
//将此数组放在磁盘中
#include<iostream>
using namespace std;
#include<fstream>
int main()
{
	ofstream ofs;
	ofs.open("arr.txt",ios::out);
	if (!ofs.is_open())
	{
		cout << "file opening failure!" << endl;
	}
	//写操作
	int arr[10];
	for (int i=0;i<10;i++)
	{
		cin >> arr[i];
		ofs << arr[i] << " ";
	}
	//关闭文件
	ofs.close();
	return 0;
}

6.案例2

#include<iostream>
using namespace std;
#include<fstream>
int main()
{
	ifstream ifs;
	ifs.open("arr.txt",ios::in);
	if (!ifs.is_open())
	{
		cout << "file opening failure" << endl;
	}
	//操作文件
	int arr[10];
	for (int i =0;i<10;i++)
	{
		ifs >> arr[i];
		cout << arr[i] << " ";
	}
	cout << endl;
	//选出最大值以及对应的编号
	int max = arr[0];
	int index = 1;
	for (int k =0 ;k<10;k++)
	{
		if (arr[k] > max)
		{
			max = arr[k];
			index = k + 1;
		}
	}
	cout << max << endl;
	cout << index << endl;
	//关闭文件
	ifs.close();
	return 0;
}

7.案例3

//从键盘上读入一行字符,把其中的字母依次存放在磁盘f2.dat中。
//再把它走磁盘文件读入程序,将其中的小写字母转换为大学字母,再存放在f3.dat中。
#include<iostream>
using namespace std;
#include<fstream>
//走键盘上读入数据,将字母存入f2.txt文件中。
void save_to_file()
{
	ofstream ofs;
	ofs.open("f2.txt",ios::out);
	if (!ofs.is_open())
	{
		cout << "f2 open error!" << endl;
	}
	char ch[80];
	cin.getline(ch,80);
	for (int i=0;ch[i]!=0;i++)
	{
		if (ch[i] >= 65 && ch[i] <= 90 || ch[i] >= 97 && ch[i] <= 122)
		{
			ofs.put(ch[i]);//这里为什么是put函数
		}
	}
	cout << endl;
	ofs.close();
}
//从f2中读出字母,将小写字母转换为大写字母,并存储在f3中。
void get_from_file()
{
	ifstream ifs;
	ifs.open("f2.txt",ios::in);
	if (!ifs.is_open())
	{
		cout << "f2 open error!" << endl;
	}
	ofstream ofs;
	ofs.open("f3.txt",ios::out);
	if (!ofs.is_open())
	{
		cout << "f3 open error!" << endl;
	}
	char c;
	while (ifs.get(c))
	{
		if (c >= 97 && c <= 122)
		{
			c = c - 32;
		}
		ofs.put(c);
		cout << c;
	}
	cout << endl;
	ifs.close();
	ofs.close();
}
int main()
{
	save_to_file();
	get_from_file();
	return 0;
}

8.案例4

#include<iostream>
using namespace std;
#include<fstream>
struct Student
{
	int Id;
	char Name[20];
	int Chinese;
	int Math;
	int Engilsh;
};
void save_to_file()
{
	ofstream ofs;
	ofs.open("s1.txt",ios::out|ios::binary);
	if (!ofs.is_open())
	{
		cout << "s1.txt open error!" << endl;
	}
	struct Student s[10];
	s[0] = { 1001,"jerry",99,99,99 };
	s[1] = { 1002,"mark",99,100,99 };
	s[2] = { 1003,"lili",89,99,99 };
	for (int i=0;i<3;i++)
	{
		ofs.write((const char*)&s[i], sizeof(Student));
	}
	ofs.close();
}
void get_from_file()
{
	struct Student s[10];
	ifstream ifs;
	ifs.open("s1.txt",ios::in|ios::binary);
	if(!ifs.is_open())
	{ 
		cout << "s1.txt open error!" << endl;
	}
	for (int i = 0; i < 3; i++)
	{
		ifs.read((char*)&s[i], sizeof(Student));
		cout << s[i].Id << " " << s[i].Name << " " << s[i].Chinese << " "
			<< s[i].Math << " " << s[i].Engilsh << endl;
	}
	ifs.close();
}
void copy_file()
{
	//先把s1中的文件读出,放在结构体数组中。
	struct Student s[10];
	ifstream ifs;
	ifs.open("s1.txt", ios::in | ios::binary);
	if (!ifs.is_open())
	{
		cout << "s1.txt open error!" << endl;
	}
	for (int i = 0; i < 3; i++)
	{
		ifs.read((char*)&s[i], sizeof(Student));
		cout << s[i].Id << " " << s[i].Name << " " << s[i].Chinese << " "
			<< s[i].Math << " " << s[i].Engilsh << endl;
	}
	//再将结构体数组中的数据放s2文件中
	ofstream ofs;
	ofs.open("s2.txt", ios::out | ios::binary);
	if (!ofs.is_open())
	{
		cout << "s2.txt open error!" << endl;
	}
	for (int i = 0; i < 3; i++)
	{
		ofs.write((const char*)&s[i], sizeof(Student));
	}
	ofs.close();
	ifs.close();
}
int main()
{
	//save_to_file();
	//get_from_file();
	copy_file();
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值