c++ 文件的操作

#include<iostream>
#include<fstream>
#include<string>
#include<iomanip>

#define N 20

using namespace std;

//标准输入输出
void test2()
{

	//标准输入
	//double a, b;
	//cin >> a >> b;


	//char ch = cin.get();
	//putchar(ch);
	//getchar();
	//cout << endl
	//cout << "second called:" << endl;
	//cin.get(ch);
	//cout << ch << endl;
	//getchar();

	//char str[10];
	//cout << "third call:";
	//cin.get(str, 6, '4');
	//cout << str << endl;
	//getline()函数
	//char str[N];
	//cout << "Enter a sentenc:";
	//cin.getline(str, 10, '/');
	//cout << "The first part" << str << endl;
	//cin.getline(str, 10);
	//cout << "The second part " << str << endl;

	//标准输出 ostream
	//cout.put(字符变量或者常量)
	//char ch = 'c';
	//cout.put(ch);
	//cout.put('a').put('c').put('d') << endl;
	//输出格式
	//输出格式的控制字符
	//dec 十进制 hex 十六进制 oct 八进制 
	//setw(n)设置输出项域宽
	//int i = 1024, j = 256,a=18;
	//double p = 3.155544, q = 3.14;
	//cout << i << endl;
	//cout << setw(15) << i << j << endl;
	//cout << hex << i <<endl;
	//cout << j << endl;
	//cout << a<< endl;
	//cout << hex << j << endl;
	//cout << setw(10) << setiosflags(ios::left) << setfill('#') << i << endl;
	//cout << setw(6) << setfill('*') <<i<< endl;
	//cout << p << endl;
	//cout << setprecision(5) << p << endl;
	//cout << p << endl;
	//cout << resetiosflags(ios::left) << setw(10) << setfill('#') << j << endl;
	//常见输出格式的成员函数
// width(n)设置当前高度n
//fill(c)填充字符c
//precision(n)设置浮点数精度n
	//int i = 1024;
	//double j = 13.14155427;
	//cout << i << endl;
	//cout.width(10);
	//cout << i << endl;
	//cout << i << endl;
	//cout.width(10);
	//cout.fill('$');
	//cout.setf(ios::left, ios::adjustfield);
	//cout << i << endl;
	//cout.width(12);
	//cout.setf(ios::right, ios::adjustfield);
	//cout.precision(5);
	//cout << j << endl;
	//cout << "width" << cout.width() << endl;

	//文件的输入输出
	//打开文件方式
	// ios::app 追加模式
	//ios::ate 打开文件时文件指针定位到文件尾
	// ios::in 打开文件进行读操作 不存在报错
	//ios::out 打开文件进行写操作 已存在更新
	//ios::trunc 打开文件 如果文件已经存在则清空原文件
	//ios::binary 二进制文件
	// 读文件
//	ifstream infile;
//	infile.open(字符串路径,打开文件方式);
或者
//	ifstream infile(字符串路径, 打开文件方式);
	// 读文件
//	ofstream outfile;
//	outfile.open(字符串路径,打开文件方式);
或者
//	ifstream outfile(字符串路径, 打开文件方式);
	//关闭文件
	//outfile.close()或者infile.close()
	//顺序文件访问
	//向输出文件中写入文本

	//fstream ofs;
	//ofs.open("d:\\fl.txt", ios::out);
	//if (ofs.fail())
	//{
	//	cout << "错误" << endl;



	//}
	//ofs << setiosflags(ios::left) << setw(13) << "Name" << setw(10) << "Class" << setw(10) << "Age" << endl;
	//ofs << setiosflags(ios::left) << setw(12) << "liu yanchao" << setw(10) << endl;
	//for (char ch = 'a';ch < 'z';ch++)
	//{
	//	ofs.put(ch);

	//}
	//ofs << endl;
	//ofs.write("sdafasdfas", 11);
	//ofs.close();
	//读取文件内容到显示器

	//fstream ifs("d:\\fl.txt", ios::out);
	//if (ifs.fail())
	//{
	//	cout << "error" << endl;

	//}
	//while (! ifs.eof())
	//{
	//	cout.put((char)ifs.get());

	//}
//用二进制方式写入数据
	//fstream ofs;
	//ofs.open("d:\\fl.txt", ios::out | ios::binary);
	//if (!ofs)
	//{
	//	cout << "错误" << endl;

	//}
	//double number[] = { 1.12,12.12,1.23,232.2 };
	//for (int i = 0;i < 4; i++)
	//{
	//	ofs.write((char *) & number[i], sizeof(double));


	//}
	//ofs.close();
//用二进制方式读数据
	//fstream ifs("d:\\fl.txt",ios::in|ios::binary);
	//if (!ifs)
	//{
	//	cout << "错误" << endl;

	//}
	//double z[4];
	//for (int i = 0;i < 4;i++)
	//{
	//	ifs.read((char*)&z[0], sizeof(double));
	//	cout << z[0] << endl;

	//}
	//ifs.close();




//随机文件访问
//seekg()用于输入文件 格式 : seekg(offset,dir) 或者 seekg(pos)
//seekp()用于输出文件 格式 : seekp(offset,dir) 或者 seekg(pos)
//ios::beg 从文件起始位置开始
//ios::cur 从当前文件指针位置开始
//ios::end 从文件结束位置开始
//tellg() 获取此时文件指针位置信息
	struct student
	{
		string name;
		int age;
		double score;
	};

	//student stu1[] = {
	//	"liu",12,90,
	//	"wang",15,60,
	//	"li",15,20
	//};
 //
	//ofstream  ofs("d:\\dl.txt", ios::out | ios::binary);
	//if (!ofs)
	//{
	//	cout << "error" << endl;

	//}
	//for (int i = 0;i < 3;i++) ofs.write((char*)&stu1[i], sizeof(stu1));
	//ofs.close();

	//ifstream ifs;
	//ifs.open("d:\\dl.txt", ios::out | ios::binary);
	//if (!ifs)
	//{
	//	cout << "error" << endl;
	//}
	//student stu2[3];
	//for (int i = 0;i < 3;i++)
	//{
	//	ifs.seekg(i*sizeof(stu2[i]),ios::beg);
	//	ifs.read((char*)& stu2[i], sizeof(student));
	//	cout << stu2[i].name << stu2[i].age  <<  stu2[i].score<< endl;


	//}
	//ifs.close();

	//读取随机文件中的数据

	//fstream fs("d:\\dl.txt",ios::out|ios::in|ios::binary);
	//if (!fs)
	//{
	//	cout << "错误" << endl;

	//}
	//for (int i = 1;i < 100;i++)
	//{
	//	fs.write((char*)&i, sizeof(int));


	//}
	//int n, m;
	//cin >> n;
	//fs.seekg(n * sizeof(int), ios::beg);
	//cout << fs.tellg() << endl;
	//fs.read((char*)&m, sizeof(int));
	//cout << "读出数据" << m<< endl;
	//数据写入随机文件
	fstream fs("d:\\dl.txt", ios::in|ios::out|ios::binary);
	if (!fs)
	{
		cout << "erroe" << endl;
	}
	for (int i = 0;i <= 10;i++)
	{
		fs.write((char*)&i, sizeof(int));
	}
	int n;
	cin >> n;
	fs.seekp(n * sizeof(int), ios::beg);//写入设置文件指针
	cout << fs.tellg() << endl;
	int m = 100;

	fs.write((char*)&m, sizeof(int));
	fs.seekg(0, ios::beg);//写出设置文件指针
	cout << fs.tellg() << endl;//输出0
	int number;
	for (int i = 0;i <=10;i++)
	{
		fs.read((char*)&number, sizeof(int));
		cout << number << endl;

	}
	fs.close();


}



//写文件
void test()
{
	ofstream ofs;
	ofs.open("d:\\fl.txt", ios::binary | ios::app);
	ofs << "姓名" << endl;
	ofs << "性别" << endl;
	ofs.close();

	return;

	
}





//读文件
void test1()
{

	ofstream ifs;
	ifs.open("d:\\fl.txt", ios::in);
	if (!ifs.is_open())
	{
		cout << "文件打开失败" << endl;

	}
	char buf[1024] = { 0 };


}


int main()
{
	test2();


	return 0;
}

  • 6
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

m0_53944811

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值