C++ C++;逐行阅读并逐行输出

C++ C++;逐行阅读并逐行输出,c++,io,C++,Io,假设我有一个文本文件,该文本文件包含以下内容: 你好,世界 欢迎来到C++ 如何从.txt文件中逐行打印?例如,这是我代码的一部分 while (getline(input, document)) { if (!document.empty()) { if (lineisthere(document)) { cout << "The word" << // << "is there" << e

假设我有一个文本文件,该文本文件包含以下内容:
你好,世界
欢迎来到C++

如何从.txt文件中逐行打印?例如,这是我代码的一部分

while (getline(input, document))
{
    if (!document.empty())
    {
        if (lineisthere(document)) {
            cout << "The word" << // << "is there" << endl;
        } else {
            cout << "The word" << // << "is not there" << endl;
        }
        line++;
    }
}
input.close(); //closes the input
while(getline(输入,文档))
{
如果(!document.empty())
{
如果(行在此处(文档)){

cout看起来您只是想在您指示的
/
位置使用
文档

cout << "The word " << document << " is there" << endl;
cout试试这个:

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

int main () 
{
  string text;
  ifstream ifs("hello.txt");


    while(!ifs.eof()) 
    {
      getline(ifs,text);
      cout << "" << text << "\n" ;
    }

  return 0;
}
#包括
#包括
#包括
使用名称空间std;
int main()
{
字符串文本;
ifstream ifs(“hello.txt”);
而(!ifs.eof())
{
getline(ifs,文本);

cout此代码正在逐行打印文本文件:

    #include<iostream>
#include<fstream>
#include<cstdlib>
#include<iomanip>
using namespace std;
int main()
{
string filename;
ifstream file;
    cout<<"enter file name";
    getline(cin,filename);
     filename.append(".txt");
    file.open(filename.c_str());


    string text;
    while(getline(file,text))
    {
        cout<<text<<endl;
    }
file.close();

    return 0;
}
#包括
#包括
#包括
#包括
使用名称空间std;
int main()
{
字符串文件名;
ifstream文件;

可能说不存在的东西有点难,你必须知道每一行上应该有什么。@Dave:大概是
lineisthere()
函数以某种方式执行该任务。@Greg Hewgill…通过引用获取
文档
,并用可能不存在的正确行填充它?我想我们可以这样假设。并且有一些静态行计数器来知道它是哪一行testing@Dave:我不知道你要去哪里。有一个
getline()
在那里,它可能从输入文件中读取一行。我想他希望能够删除其中一行,并让他的输出告诉他它丢失了。getline无法读取不存在的行,来告诉您它丢失了。我真的不知道这里问的是什么。“Hello world”与“Hello world”有什么不同欢迎来到C++"?函数
lineisthere
应该如何区分什么“存在”和什么“不存在”。我想帮助你,因为你是我见过的第一个正确使用
getline
的人,但如果我不明白,我就帮不上忙。它只检查hello world是否在第一行,还检查Welcome toC++是第二行。格雷戈回答了我所要找的。很高兴他回答你。我仍然不知道你在问什么,或者他回答的问题是什么。格雷戈显然是比我聪明的人。