C++ 如何逐行读取文件或一次读取整个文本文件?

C++ 如何逐行读取文件或一次读取整个文本文件?,c++,iostream,fstream,file-handling,C++,Iostream,Fstream,File Handling,我正在学习介绍文件的教程(如何从文件中读取和写入文件) 首先,这不是家庭作业,这只是我寻求的一般帮助 我知道如何一次读一个单词,但我不知道如何一次读一行,或者如何阅读整个文本文件 如果我的文件包含1000个单词怎么办?逐字读取整个文件是不实际的 我的名为“Read”的文本文件包含以下内容: 我喜欢玩游戏 我喜欢读书 我有两本书 这就是我迄今为止所取得的成就: #包括 #包括 使用名称空间std; int main(){ 河流充填; infle.open(“Read.txt”); 填充>> 有

我正在学习介绍文件的教程(如何从文件中读取和写入文件)

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

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

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

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

我喜欢玩游戏
我喜欢读书
我有两本书
这就是我迄今为止所取得的成就:

#包括
#包括
使用名称空间std;
int main(){
河流充填;
infle.open(“Read.txt”);
填充>>

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

您可以使用
std::getline

#include <fstream>
#include <string>

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

我认为您可以使用istream.read()函数。您可以使用合理的块大小循环,直接读取内存缓冲区,然后将其附加到某种任意内存容器(如std::vector)。我可以写一个例子,但我怀疑您是否想要一个完整的解决方案;如果您需要任何其他信息,请告诉我。

另一个尚未提及的方法是
std::vector

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

while(file >> mystr)
{
   line.push_back(mystr);
}
std::矢量线;
while(文件>>mystr)
{
行。推回(mystr);
}

然后,您可以简单地迭代向量并修改/提取您需要的内容/

我知道这是一个非常古老的线程,但我还想指出另一种非常简单的方法……这是一些示例代码:

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

int main() {

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

    while(file >> content) {
        cout << content << ' ';
    }
    return 0;
}
#包括
#包括
#包括
使用名称空间std;
int main(){
ifstream文件(“filename.txt”);
字符串内容;
while(文件>>内容){

CUT< P>,这样做也可以使用C++中提供的FrOPEN函数,并逐行读取文件:
#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;
int main(){
freopen(“文件路径”,“rb”,标准输入法);
弦线;
while(getline(cin,line))

couthello bro这是一种使用此代码读取精确行中字符串的方法

希望这能帮助你

#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;
}
#包括
#包括
使用名称空间std;
int main(){
字符串文本[1];
国际线路号;
ifstream文件(“text.txt”);
cout>lineno;
对于(int i=0;icout您也可以使用此命令逐个读取文件中的所有行,然后打印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;
}
#包括
#包括
使用名称空间std;
布尔检查文件为空(ifstream&file){
返回文件.peek()==EOF;
}
int main(){
字符串文本[256];
国际线路号;
ifstream文件(“text.txt”);
int num=0;
而(!check_file_为空(文件))
{    
getline(文件,文本[num]);
num++;
}
for(int i=0;i这里是否有明确的答案:逐字阅读可能比逐行阅读慢一点。如果你真的需要单词,那么最好是逐行阅读。如果你处理的是面向行的数据,如CSV文件,请逐行阅读。@Arkady这是不正确的。对于一个100 MiB文件,逐行阅读很容易需要时间秒,在不到一秒的时间内读取一个4千字节的块。@Vallentin:假设流都已缓冲,实际的磁盘读取已经逐块完成。其余的只是操作内存中的数据。
vector
是一个不必要的步骤。您可以使用
std::istrea来迭代
ifstream
m_iterator(infle)
。我不知道是谁否决了这个答案,但这很好,可能是我不符合你的标准,但我使用了相同的东西。很好的答案,我使用了stringstream,而不是cout来将整个文件变成一个巨大的stringstream,虽然不明显,
while(getline(f,line)){…}
确实是推荐的方法。这里解释道:---在这里你还可以找到正确错误处理的有用方法。如果没有
\include
@Tyguy7,上面的代码就不会编译。为什么需要
\include
呢?我觉得
就足够了。如果你是说
std::get行
,它在
中,而不是在
@fabioturate中。我不确定,我只知道一旦我把它包括进去,一切都会好起来。
#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;
}