-1
我想读取文件中的数据行。我所尝试的一切都不起作用。使用sscanf从字符串中读取多个值
这里是什么样的数据看起来像一个例子:
“< X = 128 Y = 480 IMG = IMG/A.png键入=差强人意>”
现在我想读4个值从这个字符串。
char img[256];
char ObjectType[256];
sscanf_s(lines[i].c_str(),"< x = %i y = %i img = %s type = %s >",&x,&y,img,ObjectType);
但每次我得到Memoryleaks。
你有什么想法吗?
编辑:
我用我的opionion不洁方法解决了我的问题。我用istringstream:
istringstream iss(lines[i]);
iss >> temp >> temp >> temp >>
x >> temp >> temp >>
y >> temp >> temp >>
img >> temp >> temp >>
ObjectType;
您不能修改'c_str()'给出的缓冲区。它返回一个'const char *'出于某种原因。 – chris
@chris这是什么意思?我只得到一个字符串,所以我必须使用c_str() –
首先,如果它存储在常量内存中,修改它是未定义的行为。标有“const”的事实是一个很好的指示。从C++ 11开始,你可以安全地使用'&s [0]',但是你不能安全地覆盖最后的null或之后的任何东西。 – chris