Click here to Skip to main content
15,998,219 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi i am try to read from a text file line by line and so i try to use getline() function

C++
#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]
Posted
Updated 10-Dec-20 8:19am
v6
Comments
Sergey Alexandrovich Kryukov 18-Feb-11 22:33pm    
Sorry, this is not your real code.
You report is invalid. There is no problem in the line you point out.
You complain about one code, show something different. (Probably you typed your question manually instead of using doing paste and made some other mistakes?)
Nothing to discuss here... show you real code, than ask your question.
--SA
Olivier Levrey 19-Feb-11 11:46am    
You were a little bit harsh with OP... As a matter of fact, there really was a problem in OP's code.
Sergey Alexandrovich Kryukov 19-Feb-11 16:39pm    
I know the problem, did not want to answer. You and Andrew answered. And maybe I was quite harsh, but I think I should be.

Let's see. Did you see the question in the version at the time of my comment?
I looked at the code and saw the errors in "include", and "t.txe" was without quotation mark.
So the code would not compile, but it could not possibly show the reported error.

So, OP has a problem of some version of the code, but did not care even about putting proper version of the code and the error. This is not being a novice, this is about being careless.

Why should we answer a confusing question? OP is most interesting, why going through all that?

And probably my call to OP worked; at least quotation problem was fixed in next version of the Question.

Do you see my point now?
So get my "5" for your answer but please understand my view: it's always better to make OP doing better job, for everyone's sake and mostly for the sake of OP.

--SA
Olivier Levrey 20-Feb-11 5:59am    
I see. Since I didn't see the first version, I couldn't understand that. It's true that a few OP's don't take enough time to post understandable or proper questions. Forget my comment then, and thank you for the vote by the way.
Sergey Alexandrovich Kryukov 20-Feb-11 14:06pm    
You're welcome, and thank you for understanding.
--SA

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. :)
 
Share this answer
 
v3
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>
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 19-Feb-11 16:40pm    
I voted 5, my comment is in reply to yours, on top of the page.
--SA
Your #includes are wrong

They should be
#include <iostream>
#include <conio.h>
#include <string>
#include <fstream>
 
Share this answer
 
Comments
Olivier Levrey 19-Feb-11 11:42am    
Good catch. My 5.
Sergey Alexandrovich Kryukov 19-Feb-11 16:42pm    
My 5. I did not want to answer, just commented to demand more responsibility from OP in asking questions, please see my comment on top (and the one in response to Olivier's).
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900