By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
431,649 Members | 696 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 431,649 IT Pros & Developers. It's quick & easy.

Problem with std::basic_istream<_Elem,_Traits> &std::getline()

P: n/a
I have a method in a class implemented like this:

int foo (const char* filename, bool flg)
{
FILE *fp;
char *buffer, *tokstr, *tmp;
size_t size, toksize;
unsigned int i;

if ((fp = fopen(filename, "r")) == NULL)
return FILE_READ_ERROR;

if (flg) {
buffer = NULL;

if (getline(&buffer, &size, fp) == -1) { // <- COMPILER BARFS HERE
!
......
}
}
}
Error message I'm getting is this:

myfile.cpp(1033): error C2780: 'std::basic_istream<_Elem,_Traits>
&std::getline(std::basic_istream<_Elem,_Traits>
&,std::basic_string<_Elem,_Traits,_Alloc> &)' : expects 2 arguments - 3
provided

Any ideas how I can use getline using file pointer?

Apr 13 '06 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Hello,
Any ideas how I can use getline using file pointer?


Nope, try this instead:

#include <string>
#include <fstream>

....
std::ifstream f("myfile.txt");
std::string line;
std::getline(f, line);
....

Good luck,
Tilman
Apr 13 '06 #2

P: n/a
Bit Byter wrote:
I have a method in a class implemented like this:

int foo (const char* filename, bool flg)
{
FILE *fp;
char *buffer, *tokstr, *tmp;
size_t size, toksize;
unsigned int i;

if ((fp = fopen(filename, "r")) == NULL)
return FILE_READ_ERROR;

if (flg) {
buffer = NULL;

if (getline(&buffer, &size, fp) == -1) { // <- COMPILER BARFS HERE
!
......
}
}
}
Error message I'm getting is this:

myfile.cpp(1033): error C2780: 'std::basic_istream<_Elem,_Traits>
&std::getline(std::basic_istream<_Elem,_Traits>
&,std::basic_string<_Elem,_Traits,_Alloc> &)' : expects 2 arguments - 3
provided

Any ideas how I can use getline using file pointer?


You can't - you are trying to mix two different parts of the C++
standard libraries, stdio and IOstreams. For stdio, use fgets instead of
getline. For IOStreams, use std::ifstream rather than FILE*.

Tom
Apr 13 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.