读取文件内容时出现问题
我有一个包含文本的文件。我逐行读取整个文件并附加到字符串对象。但是当我打印出最终的字符串时,我没有得到整个文件内容。我确信这是由于存在特殊字符,如 '\n'、'\r'、'\t' 等。
这是我的示例代码:
// Read lines until end of file (null) is reached
do
{
line = "";
inputStream->read_line(line);
cout<<"\n "<<line;//here i get the content of each line
fileContent.append(line);// here i am appending
}while(line.compare("") != 0);
I have a file which contains text. I read line by line of the entire file and append to a string object. But when i get the final string print out i am not getting the whole file content. I am sure it is due to the presence of special characters like '\n', '\r', '\t', etc.
here is my sample code:
// Read lines until end of file (null) is reached
do
{
line = "";
inputStream->read_line(line);
cout<<"\n "<<line;//here i get the content of each line
fileContent.append(line);// here i am appending
}while(line.compare("") != 0);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是 C++ 中将文件读入内存的方法:
This is the way to read a file into memory in C++:
您必须向我展示更多代码才能知道您的问题是什么。
如果您将整个文件读入单个字符串,这是我通常使用的方法:
You’ll have to show more code for me to know what your problem is.
If you’re reading the entire file into a single string, this is the method I usually use: