C++使用ifstream读取文件内容

本文介绍C++中使用ifstream类进行文件读取的方法,包括按行读取、按词读取及文件名正确性检测等操作,并提供完整示例代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

测试文件如下内容:myfile.txt

Fry: One Jillion dollars.
 [Everyone gasps.]
 Auctioneer: Sir, that's not a number.
 数据读取, 测试 。

C++中使用ifstream类实现读文件操作,代码如下:
实现了:
1、以行读取文件
2、逐个词读取文件
3、文件名正确性检测

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

using namespace std;

//读取整个文件内容到char array数组中去
void fileReadAllToCharArray()
{
	std::ifstream file;
	//以只读方式打开文件
	file.open("myfile.txt", std::ios::in);

	//指针定位到文件末尾
	file.seekg(0, std::ios::end);
	int fileLength = file.tellg();

	//指定定位到文件开始
	file.seekg(0, std::ios::beg);

	cout << "fileLength:" << fileLength << endl;
	char* buffer = new char[fileLength + 1];
	file.read(buffer, fileLength);
	buffer[fileLength] = '\0';
	string contents = buffer;
	cout << "contents:" << contents << endl;

	if (buffer) {
		delete[] buffer;
	}
	file.close();
}

//读取方式:逐行读取Line by Line, 将行读入字符数组, 行之间用回车换行区分
void fileReadToCharArray()
{
	std::ifstream file("myfile.txt");
	
	constexpr int LINE_LENGTH = 100;
	char str[LINE_LENGTH];

	int lineNum = 0;
	while (file.getline(str, LINE_LENGTH))
	{
		cout << "Read from line[" << ++lineNum << "] :"<<str<<endl;
	}
	cout << "file has line:" << lineNum << endl;
}

//读取方式:逐行读取Line by Line, 将行读入string, 行之间用回车换行区分
void fileReadToString()
{
	std::ifstream file("myfile.txt");

	int lineNum = 0;
	string str;
	while (getline(file, str)) {
		cout << "Read Data on Line:[" << ++lineNum<<"] :" << str <<endl;
	}
	cout << "file has line:" << lineNum << endl;
}

//读取方式:逐词读取Word by Word,词之间用空格划分
void fileReadWbW()
{
	std::ifstream file("myfile.txt");
	string s;
	while (file >> s)
	{
		cout << "Read From File[" << s <<"]"<<endl;
	}
}

//带检测文件名功能
void fileReadWithErrCheck()
{
	string fileName = "file .dat";
	std::ifstream fin(fileName.c_str());
	if (!fin) {
		cout << "Error Opening file:[" << fileName << "]" << " for input " << endl;
		exit(-1);
	}
}
int main()
{
#if  0
	char data[100];

	ofstream outfile;
	outfile.open("myfile.txt", ios::out | ios::trunc);

	cout << "enter your name: ";
	//cin.getline(data, 100);
	outfile << "hello world"<<endl;

	ifstream infile;
	infile.open("myfile.txt", ios::in);
	cout << "read file from myfile.txta" << endl;
	string readData;
	infile >> readData;
	std::cout << "data:" << readData << endl;
	outfile.close();
#endif

	//读取整个文件内容到char array数组中去
	fileReadAllToCharArray();
	std::cout << "-----------------" << endl;

	//逐行读取Line by Line
	fileReadToCharArray();
	std::cout << "-----------------" << endl;

	//文件逐词读取Word by Word
	fileReadWbW();
	std::cout << "-----------------" << endl;

	//逐行读取Line by Line, 将行读入string分
	fileReadToString();

	//带检测文件名功能
	fileReadWithErrCheck();
	std::cout << "-----------------" << endl;
}

其他类似实现:
1、C++ ifstream、sstream按行读取txt文件中的内容,并按特定字符分割,不用strtok

2、C++ ifstream 文件流的方式读取文件

3、C++使用ifstream read读文件时读的大小的问题

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值