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

B

Bit Byter

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?
 
T

Tilman Kuepper

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
 
T

Tom Widmer

Bit said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top