[英]C++ read line by line and output each lines
Lets say I have a text file, and the text file contains the following:假设我有一个文本文件,该文本文件包含以下内容:
hello world你好世界
Welcome to C++欢迎使用 C++
How would I print line by line from my .txt file?如何从我的 .txt 文件中逐行打印? For example, this is part of my code例如,这是我的代码的一部分
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
I want my output to look something like this:我希望我的输出看起来像这样:
The word Hello Word is there你好这个词就在那里
However, the word Welcome to C++ is not there但是,欢迎使用 C++一词并不存在
看起来您只想使用您指定的document
//
:
cout << "The word " << document << " is there" << endl;
Try this:试试这个:
#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;
}
this code is printing text file line by line:此代码逐行打印文本文件:
#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;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.