我在一个介绍文件的教程中(如何从\到文件读取和写入)
首先,这不是一个功课,这只是我正在寻求的一般帮助.
我知道如何一次读一个单词,但我不知道如何一次读一行或如何读取整个文本文件.
如果我的文件包含1000个单词怎么办?阅读每个单词是不切实际的.
我的名为(Read)的文本文件包含以下内容:
我喜欢玩我喜欢阅读的游戏我有2本书
这是我迄今为止所取得的成就:
#include <iostream>
#include <fstream>
using namespace std;
int main (){
ifstream inFile;
inFile.open("Read.txt");
inFile >>
Run Code Online (Sandbox Code Playgroud)
有没有办法一次读取整个文件,而不是分别读取每一行或每个单词?
我使用以下代码从文本文件中读取行.处理行大于限制SIZE_MAX_LINE的情况的最佳方法是什么?
void TextFileReader::read(string inFilename)
{
ifstream xInFile(inFilename.c_str());
if(!xInFile){
return;
}
char acLine[SIZE_MAX_LINE + 1];
while(xInFile){
xInFile.getline(acLine, SIZE_MAX_LINE);
if(xInFile){
m_sStream.append(acLine); //Appending read line to string
}
}
xInFile.close();
}
Run Code Online (Sandbox Code Playgroud)