[英]How to read a file line by line or a whole text file at once?
我在一個介紹文件的教程中(如何從文件中讀取和寫入文件)
首先,這不是作業,這只是我正在尋求的一般幫助。
我知道如何一次閱讀一個單詞,但我不知道如何一次閱讀一行,也不知道如何閱讀整個文本文件。
如果我的文件包含 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 >>
有沒有辦法一次讀取整個文件,而不是分別讀取每一行或每個單詞?
您可以使用std::getline
:
#include <fstream>
#include <string>
int main()
{
std::ifstream file("Read.txt");
std::string str;
while (std::getline(file, str))
{
// Process str
}
}
另請注意,最好在其構造函數中使用文件名構造文件流,而不是顯式打開(關閉也是如此,只需讓析構函數完成工作)。
有關std::string::getline()
更多文檔可以在CPP 參考 中閱讀。
讀取整個文本文件的最簡單方法可能只是連接那些檢索到的行。
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');
}
我知道這是一個非常古老的線程,但我還想指出另一種實際上非常簡單的方法......這是一些示例代碼:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main() {
ifstream file("filename.txt");
string content;
while(file >> content) {
cout << content << ' ';
}
return 0;
}
我認為你可以使用 istream .read() 函數。 您可以使用合理的塊大小循環並直接讀取到內存緩沖區,然后將其附加到某種任意內存容器(例如 std::vector)。 我可以寫一個例子,但我懷疑你想要一個完整的解決方案; 如果您需要任何其他信息,請告訴我。
好吧,要做到這一點,也可以使用 C++ 中提供的 freopen 函數 - http://www.cplusplus.com/reference/cstdio/freopen/並按如下方式逐行讀取文件 -:
#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;
}
另一種尚未提及的方法是std::vector
。
std::vector<std::string> line;
while(file >> mystr)
{
line.push_back(mystr);
}
然后您可以簡單地遍歷向量並修改/提取您需要的內容/
你好兄弟這是一種使用此代碼讀取確切行中的字符串的方法
希望這可以幫助你!
#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;
}
祝你好運 !
您也可以使用它來一一讀取文件中的所有行,然后打印 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;
}
希望這可以幫助你:)
上面的解決方案很好,但有一個更好的解決方案“一次讀取文件”:
fstream f(filename);
stringstream iss;
iss << f.rdbuf();
string entireFile = iss.str();
以下代碼段將幫助您閱讀由 unicode 字符組成的文件
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);
讀完后一定要關閉文件指針,否則會報錯
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.