服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - C/C++ - C++ 读文件 将文件内容读入到字符串string中的方法

C++ 读文件 将文件内容读入到字符串string中的方法

2021-06-28 10:53奋斗_小伙 C/C++

今天小编就为大家分享一篇C++ 读文件 将文件内容读入到字符串string中的方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧

如下所示:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <string>
#include <fstream>
#include <sstream>
#include <iostream>
 
#include <stdlib.h>
using namespace std;
 
 
//从文件读入到string里
string readFileIntoString(char * filename)
{
ifstream ifile(filename);
//将文件读入到ostringstream对象buf中
ostringstream buf;
char ch;
while(buf&&ifile.get(ch))
buf.put(ch);
//返回与流对象buf关联的字符串
return buf.str();
}
 
int main()
{
//文件名
char * fn="a.txt";
string str;
str=readFileIntoString(fn);
cout<<str<<endl;
system("pause");
 
}

以上这篇C++ 读文件 将文件内容读入到字符串string中的方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持服务器之家。

原文链接:https://blog.csdn.net/my_xxh/article/details/51278440

延伸 · 阅读

精彩推荐