简体   繁体   中英

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.

您需要sscanf()函数。

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM