iis服务器助手广告广告
返回顶部
首页 > 资讯 > 后端开发 > 其他教程 >C++使用ifstream读取文件内容的示例详解
  • 752
分享到

C++使用ifstream读取文件内容的示例详解

C++ifstream读取文件内容C++ifstream读取文件C++ifstream 2023-03-01 11:03:15 752人浏览 独家记忆
摘要

测试文件如下内容:myfile.txt Fry: One Jillion dollars. [Everyone gasps.] Auctioneer: Sir,

测试文件如下内容: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;
}

到此这篇关于C++使用ifstream读取文件内容的示例详解的文章就介绍到这了,更多相关C++ ifstream读取文件内容内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!

--结束END--

本文标题: C++使用ifstream读取文件内容的示例详解

本文链接: https://www.lsjlt.com/news/197962.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作