user207311
user207311

Reputation: 23

C++ sscanf throws "no suitable conversion" error with unrelated type at compilation

I'm using sscanf to extract floating point numbers from a stream, like this:

ifstream ifs;   
ifs.open("filename.txt");    
string line;
float x, y, z;
while (getline(ifs,line))
{
    sscanf(line, "%a %a %a", &x, &y, &z);
    //*do something with x, y and z*
}

At compilation the line beginning with "sscanf" returns:

file2.cuh(41): error: no suitable conversion function from "std::string" to "const char *" exists

My understanding is that getline fills the string "line" with a line from the stream object, which sscanf then takes as input, parsing three floats from it. Where does a const char* enter in here? I'm new to C-like languages, but my sscanf syntax and the context of its usage matches all the examples I've seen.

This is taking place inside a header file I've included in a cuda 5.5 program I'm trying to compile with nvcc, if that's relevant.

Upvotes: 2

Views: 841

Answers (1)

yizzlez
yizzlez

Reputation: 8805

sscanf is from C, so it takes a C-String, pass line.c_str() to sscanf

sscanf(line.c_str(), "%a %a %a", &x, &y, &z); //.c_str() returns the c-style string of std::string

Upvotes: 4

Related Questions