如何一行一行地读取一个文件或一个完整的文本文件?

我在一个教程,介绍文件(如何从文件读取和写入文件)

首先,这不是一个家庭作业,这只是一般的帮助,我正在寻求。

我知道如何一次读取一个单词,但我不知道如何一次读取一行,或如何读取整个文本文件。

如果我的文件包含1000个单词怎么办? 逐字逐句地阅读整个文件是不切实际的。

我名为“ Read”的文本文件包含以下内容:

I love to play games
I love reading
I have 2 books

这就是我迄今为止取得的成就:

#include <iostream>
#include <fstream>


using namespace std;
int main (){
   

ifstream inFile;
inFile.open("Read.txt");


inFile >>

有没有可能一次读取整个文件,而不是分别读取每一行或每一个单词?

457856 次浏览

You can use std::getline :

#include <fstream>
#include <string>


int main()
{
std::ifstream file("Read.txt");
std::string str;
while (std::getline(file, str))
{
// Process str
}
}

Also note that it's better you just construct the file stream with the file names in it's constructor rather than explicitly opening (same goes for closing, just let the destructor do the work).

Further documentation about std::string::getline() can be read at CPP Reference.

Probably the easiest way to read a whole text file is just to concatenate those retrieved lines.

std::ifstream file("Read.txt");
std::string str;
std::string file_contents;
while (std::getline(file, str))
{
file_contents += str;
file_contents.push_back('\n');
}

I think you could use istream .read() function. You can just loop with reasonable chunk size and read directly to memory buffer, then append it to some sort of arbitrary memory container (such as std::vector). I could write an example, but I doubt you want a complete solution; please let me know if you shall need any additional information.

Another method that has not been mentioned yet is std::vector.

std::vector<std::string> line;


while(file >> mystr)
{
line.push_back(mystr);
}

Then you can simply iterate over the vector and modify/extract what you need/

I know this is a really really old thread but I'd like to also point out another way which is actually really simple... This is some sample code:

#include <iostream>
#include <fstream>
#include <string>
using namespace std;


int main() {


ifstream file("filename.txt");
string content;


while(file >> content) {
cout << content << ' ';
}
return 0;
}

Well, to do this one can also use the freopen function provided in C++ - http://www.cplusplus.com/reference/cstdio/freopen/ and read the file line by line as follows -:

#include<cstdio>
#include<iostream>


using namespace std;


int main(){
freopen("path to file", "rb", stdin);
string line;
while(getline(cin, line))
cout << line << endl;
return 0;
}

hello bro this is a way to read the string in the exact line using this code

hope this could help you !

#include <iostream>
#include <fstream>


using namespace std;




int main (){




string text[1];
int lineno ;
ifstream file("text.txt");
cout << "tell me which line of the file you want : " ;
cin >> lineno ;






for (int i = 0; i < lineno ; i++)
{
        

getline(file , text[0]);


}


cout << "\nthis is the text in which line you want befor  :: " << text[0] << endl ;
system("pause");


return 0;
}

Good luck !

you can also use this to read all the lines in the file one by one then print i

#include <iostream>
#include <fstream>


using namespace std;






bool check_file_is_empty ( ifstream& file){
return file.peek() == EOF ;
}


int main (){




string text[256];
int lineno ;
ifstream file("text.txt");
int num = 0;


while (!check_file_is_empty(file))
{
getline(file , text[num]);
num++;
}
for (int i = 0; i < num ; i++)
{
cout << "\nthis is the text in " <<  "line " << i+1 << " :: " << text[i] << endl ;




}
    

system("pause");


return 0;
}

hope this could help you :)

The above solutions are great, but there is a better solution to "read a file at once":

fstream f(filename);
stringstream iss;
iss << f.rdbuf();
string entireFile = iss.str();

The below snippet will help you to read files which consists of unicode characters

CString plainText="";
errno_t errCode = _tfopen_s(&fStream, FileLoc, _T("r, ccs=UNICODE"));
if (0 == errCode)
{
CStdioFile File(fStream);
CString Line;
while (File.ReadString(Line))
{
plainText += Line;
}
}
fflush(fStream);
fclose(fStream);

you should always close the file pointer after you read, otherwise it will leads to error