having trouble with fstream ifstream printing?


I used g++ compiler in linux.
This is the error I got back:


readtxt.cpp: In function ‘int main()’:
readtxt.cpp:28: error: no matching function for call to ‘std::basic_ifstream<char, std::char_traits<char> >::getline(std::istream&, std::string&)’
/usr/include/c++/4.3/istream:598: note: candidates are: std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::getline(_CharT*, std::streamsize, _CharT) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.3/istream:409: note: std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::getline(_CharT*, std::streamsize) [with _CharT = char, _Traits = std::char_traits<char>]


This is the code I am using:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include <iostream>
#include <string>
#include <fstream>
using namespace std;

int main()
{

string filename;
string input;
int n, i; //i = loop counter, n = array index for quitting program

cout << "Enter a filename to read input from:";
getline(cin, filename);

ifstream krack;
krack.open(filename.c_str());
if (! krack)
{
cout << "File " << filename << " could not be opened.";
return -1;
}

while(1)
{
for (i = 1; i <= 24 && ! krack.eof(); i++)
{
krack.getline(cin, input);
cout << input << endl;
}
if (krack.eof())
break;
cout << "More?";
getline(cin, input);
n = input[0];
if (n == 'q' || 'Q')
break;
}
return 0;
}
Use getline( krack, input ); on line 28 -not istream member getline-
It's std::getline(), not object.getline(). If you want to getline() from a stream other than std::cin, you just do std::getline(stream,string)
thanks
@helios:ofc u can do fstream.getline(buffer, count, delim)...
Last edited on
Yes, but that version sucks.
Topic archived. No new replies allowed.