C++ 当我找到行尾时,如何停止阅读
我试图从一个文件中读取,当我到达行的末尾时停止。问题是,它似乎不起作用_(ツ)_/“你知道为什么吗C++ 当我找到行尾时,如何停止阅读,c++,C++,我试图从一个文件中读取,当我到达行的末尾时停止。问题是,它似乎不起作用_(ツ)_/“你知道为什么吗 #include <iostream> #include <fstream> using namespace std; int main(){ char a; ifstream myfile; myfile.open("text.txt"); while (!myfile.eof()) { myfile>> a;
#include <iostream>
#include <fstream>
using namespace std;
int main(){
char a;
ifstream myfile;
myfile.open("text.txt");
while (!myfile.eof())
{
myfile>> a;
if (a=='\n')
cout << "end of line";
}
myfile.close();
}
#包括
#包括
使用名称空间std;
int main(){
字符a;
ifstreammyfile;
myfile.open(“text.txt”);
而(!myfile.eof())
{
myfile>>a;
如果(a=='\n')
是否可以尝试使用while(myfile.get(a))
while (myfile.get(a))
{
if (a=='\n')
cout << "end of line";
}
while(myfile.get(a))
{
如果(a=='\n')
是否可以尝试使用while(myfile.get(a))
while (myfile.get(a))
{
if (a=='\n')
cout << "end of line";
}
while(myfile.get(a))
{
如果(a=='\n')
cout使用for
循环
std::ifstream ifs( "file" );
for( char chr = 0; ifs.peek() != '\n'; ifs.get( chr ) ){
std::cout << chr;
}
ifs.close();
std::ifstream ifs(“文件”);
for(char chr=0;ifs.peek()!='\n';ifs.get(chr)){
std::cout使用for
循环
std::ifstream ifs( "file" );
for( char chr = 0; ifs.peek() != '\n'; ifs.get( chr ) ){
std::cout << chr;
}
ifs.close();
std::ifstream ifs(“文件”);
for(char chr=0;ifs.peek()!='\n';ifs.get(chr)){
我刚刚重写了你的代码:
#include <iostream>
#include <fstream>
using namespace std;
int main(){
char a;
ifstream myfile;
myfile.open("/Users/sijan/CLionProjects/test2/text.txt",ifstream::in);
while (myfile.get(a))
{
cout<< a;
if (a=='\n')
cout << "end of line\n";
}
if (myfile.eof())
cout << "end of file";
myfile.close();
}
#包括
#包括
使用名称空间std;
int main(){
字符a;
ifstreammyfile;
打开(“/Users/sijan/CLionProjects/test2/text.txt”,ifstream::in);
while(myfile.get(a))
{
我刚刚重写了你的代码:
#include <iostream>
#include <fstream>
using namespace std;
int main(){
char a;
ifstream myfile;
myfile.open("/Users/sijan/CLionProjects/test2/text.txt",ifstream::in);
while (myfile.get(a))
{
cout<< a;
if (a=='\n')
cout << "end of line\n";
}
if (myfile.eof())
cout << "end of file";
myfile.close();
}
#包括
#包括
使用名称空间std;
int main(){
字符a;
ifstreammyfile;
打开(“/Users/sijan/CLionProjects/test2/text.txt”,ifstream::in);
while(myfile.get(a))
{
无法为什么要使事情变得比需要的更困难。如果要分析行,请使用std::getline()
#包括
#包括
int main(int argc,char*argv[]){
std::ifstream myfile(“text.txt”);
std::字符串行;
while(std::getline(myfile,line)){
std::cout为什么要使事情变得比需要的更困难。如果要分析行,请使用std::getline()
#包括
#包括
int main(int argc,char*argv[]){
std::ifstream myfile(“text.txt”);
std::字符串行;
while(std::getline(myfile,line)){
std::coutwhile(!myfile.eof())
“似乎不起作用”不是一个问题描述。它实际上在做什么?它没有找到行的更改。永远不要键入“行尾”你是如何运行它的,它实际上在做什么?1)单击链接?它解释了原因。while(!myfile.eof())
“似乎不起作用”不是问题描述。它实际上在做什么?它没有定位行的更改。从不键入“行尾”您是如何运行它的?它实际上在做什么?1)单击链接?它解释了原因。@deW1问题中的示例是关于逐字阅读的,我认为这是有原因的。当然,有多种方法可以做到这一点,如其他答案所示。@deW1问题中的示例是关于逐字阅读的,我是这是有原因的。当然,有不止一种方法可以做到这一点,如其他答案所示。