Windows中C++读写文本文件

1.读取文本文件

(1) 使用fstream类

使用fstream类可以方便地进行文件的读取。fstream类提供了对文件输入输出的功能。可以使用ifstream类来读取文件。下面是一个简单的例子:

#include <iostream>
#include <fstream>
#include <string>
#include <cstdio>
#include <windows.h>

#pragma execution_character_set("utf-8");
using namespace std;

int main()
{
	SetConsoleOutputCP(65001);
	ifstream ifs;
	ifs.open("test01.txt", ios::in);
	if (ifs.is_open())
	{
		string line;
		while (std::getline(ifs, line))
		{
			cout << line << endl;
		}
		ifs.close();
	}
	else
	{
		cout << "文件打开失败" << endl;
	}
	return 0;
}

(2) C风格的文件操作

也可以使用C风格的文件操作函数,如fopenfread等。这些函数位于cstdio头文件中。下面是一个使用FILE结构和fread函数的例子。

#include <iostream>
#include <fstream>
#include <string>
#include <cstdio>
#include <windows.h>

#pragma execution_character_set("utf-8");
using namespace std;

int main()
{
	SetConsoleOutputCP(65001);
	FILE * file;
	errno_t err = fopen_s(&file, "test01.txt", "r");

	if (!err)
	{
		char buffer[100];
		while (fgets(buffer, sizeof(buffer), file) != nullptr)
		{
			printf("%s", buffer);
		}
		fclose(file);
	}
	else
	{
		printf("无法打开文件\n");
	}
	return 0;
}

2.写入文本文件

(1) fstream(C++风格的文件操作)

#include <iostream>
#include <fstream>
#include <string>
#include <cstdio>
#include <windows.h>

#pragma execution_character_set("utf-8");
using namespace std;

int main()
{
	SetConsoleOutputCP(65001);
	ofstream ofs("test02.txt");
	if (ofs.is_open())
	{
		ofs << "Hello World." << endl;
		ofs << "你好世界." << endl;
		ofs.close();
		cout << "文件写入成功" << endl;
	}
	else
	{
		cout << "无法打开文件" << endl;
	}
	return 0;
}

(2) C风格的文件操作

#include <iostream>
#include <fstream>
#include <string>
#include <cstdio>
#include <windows.h>

#pragma execution_character_set("utf-8");
using namespace std;

int main()
{
	SetConsoleOutputCP(65001);
	FILE * file;
	errno_t err = fopen_s(&file, "test03.txt", "w");
	if (!err)
	{
		fprintf(file, "Hello World.\n");
		fprintf(file, "你好世界.");
		fclose(file);
		std::cout << "文件写入成功" << std::endl;
	}
	else
	{
		std::cout << "无法打开文件" << std::endl;
	}
	return 0;
}

3. 处理大文件

当处理大文件时,一次性将整个文件加载到内存可能会导致内存不足或性能问题。为了避免这些问题,可以采用逐行或逐块读取的方式进行文件读取。
使用I/O流仍然是一个不错的选择,但要注意使用适当的缓冲区大小以提高效率。另外,C++标准库中的std::ifstream或C 风格的FILE指针也可以逐行或逐快读取文件。
对于逐行读取,代码如1.1所示,对于逐块读取,可以使用类似以下的方法:

#include <iostream>
#include <fstream>
#include <string>
#include <cstdio>
#include <windows.h>

#pragma execution_character_set("utf-8");
using namespace std;

int main()
{
	SetConsoleOutputCP(65001);
	std::ifstream ifs("test01.txt", std::ios::binary);
	if (ifs.is_open())
	{
		const int bufferSize = 4096;
		char buffer[bufferSize];
		while(!ifs.eof())
		{
			ifs.read(buffer, bufferSize);
			// 处理读取的数据块
			// ...
		}
		ifs.close();
	}
	else
	{
		std::cout << "无法打开文件" << std::endl;
	}
	return 0;
}
	
  • 7
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值