我已经有了一个"复杂"的程序,所以如果可能的话,我想使用以下代码:
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
}
}
现在我需要分离这样一行:
f 2/3/1 3/4/1 4/6/1
并找到了应该像一样工作的fscanf
fscanf(file, "%d/%d/%d %d/%d/%d %d/%d/%dn", &a, &b, &c, &d, &e, &f, &g, &h, &i);
但是"file"必须是一个文件。我如何将字符串(在这种情况下是sub)输入到fscanf中,或者是否有其他简单的解决方案可以尽可能短地读取这一行。
您需要sscan()函数。
sscanf(sub, "%d/%d/%d %d/%d/%d %d/%d/%dn", &a, &b, &c, &d, &e, &f, &g, &h, &i);