使用getline()时出错,尝试从文件逐行读取时 [英] Error During Using getline() , When trying to read line by line from the file

查看:540
本文介绍了使用getline()时出错,尝试从文件逐行读取时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨 我尝试逐行读取文本文件,因此尝试使用getline()函数

 #include   <   iostream.h  > 
 #include   <   conio  > 
 #include   <  字符串 > 
 #include   <   fstream.h  > 
 // 使用命名空间std; 
使用 命名空间 std;
无效 main()
{
  ifstream inFile(" );
  弦线;
  如果(inFile.fail())
   {
     cout<< " << endl;
   }
   其他
   {
    同时(!inFile.eof())
     {
       getline(inFile,line);
       cout<< " <<行<<恩德尔
     }
   }

   getch();
} 




不幸的是我得到一个错误
错误是:
错误:scheduler.cpp(17,26):找不到''std :: getline(ifstream,std :: basic_string< char,std :: string_char_traits< char> std :: allocator< char> )''

[edit]添加了代码块,取消选择了忽略HTML ..."选项-OriginalGriff [/edit]
[edit]用作<>的正确HTML实体-Emilio [/edit]

解决方案

您的#include是错误的

他们应该是

#include <iostream>
#include <conio.h>
#include <string>
#include <fstream>


安德鲁是对的.从fstream.hiostream.h中删除.h.我认为conio不是必需的,您可以将其删除.

#include <iostream>
#include <string>
#include <fstream>


在这里仍在学习,我使用char line [X],其中X是字符串的长度.我还不知道字符串实体.无论如何,

infile.getline(line,X);  



infile是流对象; getline()方法;行是要填充的字符串; X是字符串的最大长度;也许使用字符串对象,X可以替换为(strlen(line))?
如果还有另一个原型,我不知道.

如果我输入有误,希望有人会尽快纠正我. :)


hi i am try to read from a text file line by line and so i try to use getline() function

#include <iostream.h>
#include <conio>
#include <string>
#include <fstream.h>
//using namespace std;
using namespace std;
void main()
{
  ifstream inFile("t.txt");
  string line;
  if(inFile.fail())
   {
     cout<<"Error hapen when trying to open the file"<<endl;
   }
   else
   {
    while(!inFile.eof())
     {
       getline(inFile,line);
       cout << "> " << line << endl;
     }
   }

   getch();
}




unfortunately i get an error
the error is:
Error: scheduler.cpp(17,26):Could not find a match for ''std::getline(ifstream,std::basic_string<char,std::string_char_traits<char>,std::allocator<char>>)''

[edit]Code block added, "Ignore HTML..." option deselected - OriginalGriff[/edit]
[edit]Proper HTML entity used as <> - Emilio[/edit]

解决方案

Your #includes are wrong

They should be

#include <iostream>
#include <conio.h>
#include <string>
#include <fstream>


Andrew is right. Remove .h from fstream.h and iostream.h. And conio is not necessary I think, you may remove it.

#include <iostream>
#include <string>
#include <fstream>


still learning here, I use char line[X] where X is the length of the string. I dont know the string entity yet. Anyway, to the point,

infile.getline(line,X);  



infile being the stream object; getline() the method; line being your string to fill; and X being the max length of the string; perhaps with string object, X can be replaced with (strlen(line)) ??
if there is another prototype, I am unaware of it.

if I am in error, hopefully someone will correct me quickly. :)


这篇关于使用getline()时出错,尝试从文件逐行读取时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆