phil _
phil _

Reputation: 1

using fscanf or equivalent for scanning strings

i already have a "complex" program, so i want to use following code if possible:

const char* path = filename.c_str();
FILE * file = fopen(path, "r");
if( file == NULL ) {
   printf("Obj File was not found");
   return 0;
} else {
   std::ifstream input(path);
   std::string line;
   while( std::getline( input, line ) ) {
      string sub;
      iss >> sub;
      // here i read in a lot of lines which are in sub
   }
}

now i need to seperate a line like this:

f 2/3/1 3/4/1 4/6/1

and found fscanf which should work like

fscanf(file, "%d/%d/%d %d/%d/%d %d/%d/%d\n", &a, &b, &c, &d, &e, &f, &g, &h, &i);

but "file" must be a file. how can i input a string (sub in this case) into fscanf or is there another easy solution to read this line as short as possible.

Upvotes: 0

Views: 403

Answers (1)

John Hascall
John Hascall

Reputation: 9416

You want the sscanf() function.

sscanf(sub, "%d/%d/%d %d/%d/%d %d/%d/%d\n", &a, &b, &c, &d, &e, &f, &g, &h, &i);

Upvotes: 1

Related Questions