frazman
frazman

Reputation: 33223

another C++ equivalent for the followingtwo lines of code

Sorry for such a vague title. Basically, I am trying to hack a function to suit my needs. But lately I have been working a lot on python and my c++ is bit rusty.

So earlier my function took a

 int func(FILE *f)  
 { .....
   if (fgets(line, MM_MAX_LINE_LENGTH, f) == NULL) 
    return MM_PREMATURE_EOF;

if (sscanf(line, "%s %s %s %s %s", banner, mtx, crd, data_type, 
    storage_scheme) != 5)
    return MM_PREMATURE_EOF;

 }

now instead of this I am directly inputing the string data

 int func(std::string *data)  
   { .....
   // how should I modify this if statment..I want to parse the same file
   // but instead it is in form of one giant string

 }

Thanks

Upvotes: 1

Views: 89

Answers (1)

jxh
jxh

Reputation: 70392

You can use the same code, just convert the data in the std::string into a C string.

sscanf(data->c_str(), "%s %s %s %s %s", //...);

However, you should consider passing in a const reference, since you are probably not planning on modifying your input data:

int func(const std::string &data) {
    //...
    if (sscanf(data.c_str(), //...)) {
        //...
    }
}

Upvotes: 2

Related Questions