c++函数返回整行Fstream
fstream c ++ functions return full line fstream
c++语言创建一个个人网页,这里我使用C++
作为cgi输出到一个web服务器XAMP
,加载fstream
与c ++ html
分离稿件,使htmlstream
功能块在记事本中不太复杂的同时编码c ++
,问题是当一个功能htmlstream
制作时,只有一行文本,它不能显示所有的文本
#include <iostream>
#include <fstream>
using namespace std;
string htmlstream(const char* _filename){
string htmltext;
fstream open_html (_filename);
if(open_html.is_open()){
while(getline(open_html,htmltext)){
return htmltext;
}
}else{
cout<<"File: NO Reading"<<endl;
}
}
int main(){
string importhtml = htmlstream("body.html");
cout<<importhtml;
return 0;
}
只显示一行文本的原因是因为函数getline读取直到它到达当前行的末尾。也就是说,每次读取一行时,字符串变量的值都会改变。如果您希望存储每行的值,则必须在读取时附加每行。有多种解决方案,我决定用一些简单的。
看看是否有帮助。
#include <iostream>
#include <cstdlib>
#include <fstream>
#include <string>
/// Simplify calls to the standard library
using namespace std;
string htmlStream( const char *fileName )
{
string text; /// Content of current file
ifstream inFile; /// Input file stream
inFile.open( fileName ); /// Open file to read
if ( inFile.is_open() ) {
/// File successfully open, so process it
string line; /// String being read
/// Read file, line by line
while ( getline( inFile, line ) ) {
text += line;
}
}
else {
/// Could not open file, so report error to the stderr
cerr << "Cannot open "" << fileName << """ << endl;
exit( EXIT_FAILURE );
}
return text;
}
int main( int argc, const char * argv[] ) {
string str = htmlStream( "darkness.txt" );
cout << str << endl;
return EXIT_SUCCESS;
}
相关文章:
- 来自 std::list 的迭代器 .end() 按预期返回"0xcdcdcdcdcdcdcdcd"但 .begin()
- 什么时候在C++中返回常量引用是个好主意
- 你能重载对象变量名本身返回的内容吗
- 为什么 Serial.println(<char[]>);返回随机字符?
- C++映射:具有自定义类的运算符[]不起作用(总是返回0)
- 如何获取std::result_of函数的返回类型
- QueryWorkingSet总是返回false
- (C++)分析树以计算返回错误值的简单算术表达式
- 访问者访问变体并返回不同类型时出错
- 如何返回一个类的两个对象相加的结果
- OpenInventor从9.8升级到10.4.2后,GLSL纹理返回零
- C 从文件中读取FSTREAM数据不会返回正确的值.inputfile.tellg返回-1
- 为什么 std::fstream 返回 void 而不是 bool
- 为什么可以在没有返回类型的情况下运行 /usr/include/c ++/5/bits/fstream.tcc 中
- 为什么 fstream::tellg() 返回值会因输入文本文件中的换行符数而增大,当文件针对 Windows (r
- if('fstream 对象')如何返回 true 或 false 的值,具体取决于文件是否已打开?
- 为什么 fstream 方法返回对"*this"的引用?
- 即使文件名不正确,我的fstream也总是返回true
- c++函数返回整行Fstream
- 返回一个fstream