(20200422)C++文件读写操作——getline函数【与C语言的fscanf和fgets函数读取文件数据对比】

补充一个简单的读文件数据和存数据到文件 【20200501】:

使用C语言fscanf和fgets函数读取txt文件数据【这两个函数在C++中照样可以使用】

具体见:【https://blog.csdn.net/sinat_32602421/article/details/105880331】,这里再将【https://blog.csdn.net/sinat_32602421/article/details/105880331】里面的读取方式用C++的函数来读取:

 

C++语言读取文件数据——getline函数和string类:

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

struct stru
{
	char c[10];
	int a;
	char arr[10];
};


//C++向文件存入数据
void writeFile()
{
	ofstream fout;
	fout.open("0501.txt", ios::out);
	if (!fout.is_open())
	{
		cout << "文件打开失败" << endl;
		return;
	}

	fout << "1234" << ' ' << "hello" << endl;
	fout << "1234hello" << endl;
	fout << 3.1415 << ' ' << "hello" << endl;
	fout << 3.1415 << ' ' << "hello";

	fout.close();
	cout << "数据存储完成" << endl;
}

//使用C++读取文件
void ReadFile1()
{
	ifstream fin;
	fin.open("0501.txt", ios::in);
	if (!fin.is_open())
	{
		cout << "文件打开失败" << endl;
		return;
	}

	//读取第一行:直接使用fin   //ifstream fin;
	string buf1;
	getline(fin, buf1);
	cout << "line 1:" << buf1 << endl;

	//读取第2行:getline
	string buf2;
	getline(fin, buf2);
	cout << "line 2:" << buf2 << endl;
	//分解第2行
	string buf2_1;
	buf2_1.assign(buf2, 0, 4);//buf2_1 = buf2.substr(0, 4);
	cout << "line 2_1:" << buf2_1 << endl;
	string buf2_2;
	buf2_2.assign(buf2, 4, 5);//buf2_2 = buf2.substr(4, 5);
	cout << "line 2_2:" << buf2_2 << endl;


	//读取第3行和第4行,这两行格式相同
	struct stru stru1[2]; //定义结构体数组,装这两行数据
	int line = 0;
	while (!fin.eof())
	{
		string Tstr;
		getline(fin, Tstr);
		cout << "line" << line + 3 << ": " << Tstr << endl;
		strcpy(stru1[line].arr, (Tstr.substr(0, 6)).c_str()); 
		stru1[line].a = atof(stru1[line].arr); //将字符串"3.1415"转为浮点数3.1415
		strcpy(stru1[line].c, (Tstr.substr(7, 5)).c_str());
		cout << "line" << line + 3 << "_1: " << stru1[line].arr << endl;
		cout << "line" << line + 3 << "_2: " << ' ' << stru1[line].c << endl;
		line += 1;
	}

	fin.close();
	cout << "数据读取完成\n" << endl;
}



int main()

{
	writeFile();//使用C++写文件
	ReadFile1();//使用C++读取文件


	return 0;
}

说明:

向文件中存储数据:

1】

2】

3】

4】

 

从文件中读取数据:

5】

6】getline函数的使用参考【http://c.biancheng.net/view/1533.html

7】

8】

9】

 

 

 

=================================================

 

二进制文件写数据:
 

#include <iostream>
#include <fstream> //读写操作
#include <string>
using namespace std;

class person
{
public:
	char name[64];
	int age;
};


int main()

{
	//1,包含头文件

	//2,创建输出流对象
	ofstream ofs;

	//3,打开文件
	ofs.open("text.txt",ios::out|ios::binary);

	//4,写文件
	person per = { "zhangsan", 24 };
	ofs.write((const char *)&per, sizeof(per));  //&per类型转为为const char *

	//5,关闭文件
	ofs.close();



	system("pause");
	return 0;
}

说明:

二进制写数据,打开看是乱码。

 

——————————————————————————————————

二进制文件读数据:

 

#include <iostream>
#include <fstream> //读写操作
#include <string>
using namespace std;

class person
{
public:
	char name[64];
	int age;
};


int main()

{
	//1,包含头文件

	//2,创建流对象
	ifstream ifs;

	//3,打开文件
	ifs.open("text.txt",ios::in|ios::binary);
	if (!ifs.is_open())
	{
		cout << "打开失败" << endl;
		return 0;
	}

	//4,写文件
	person per;
	ifs.read((char *)&per, sizeof(per));  //&per类型转为为const char *
	cout << "name:" << per.name << endl;
	cout << "age:" << per.age << endl;

	//5,关闭文件
	ifs.close();



	system("pause");
	return 0;
}

 

 

  
 

读文本文件:

(给出四种读取方法)

#include <iostream>
#include <fstream> //读写操作
#include <string>
using namespace std;

int main()

{
	//1,包含头文件

	//2,创建流对象
	ifstream ifs;

	//3,打开文件并判断是否打开成功
	ifs.open("0422.txt",ios::in);//读文件

	if (!ifs.is_open())  //打开成功ifs.is_open(),返回真
	{
		cout << "文件打开失败" << endl;
		return 0;//打开失败,结束程序
	}


	//4,读数据
	//方法1
	//char buf[1024] = {0};  //定义一个字符数组,装读入的数据
	//while (ifs >> buf)  //一直读到文件尾,即读取所有数据,存到buf
	//{
	//	cout << buf << endl;
	//}

	//方法2
	//char buf[1024] = { 0 };
	//while (ifs.getline(buf, sizeof(buf)))
	//{
	//	cout << buf << endl;
	//}

	//方法3
	//string buf;//定义一个字符串,#include <string>
	//while (getline(ifs, buf))
	//{
	//	cout << buf << endl;
	//}

	//方法4
	char c;//定义一个字符
	while ((c = ifs.get()) != EOF)
	{
		cout << c; //<< endl;
	}



	//5,关闭文件
	ifs.close();



	system("pause");
	return 0;
}

1】

 

2】

3】

4】

 

  
20200422

写文件(文本):

#include <iostream>
#include <fstream> //读写操作
using namespace std;

int main()

{
	//1,包含头文件

	//2,创建流对象
	ofstream ofs;

	//3,指定打开方式
	ofs.open("0422.txt",ios::out);//写

	//4,写内容
	ofs << "name:zhangsan" << endl;
	ofs << "lisi" << endl;

	//5,关闭文件
	ofs.close();



	system("pause");
	return 0;
}

 

 


 

 

 

  • 2
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值