Hi Guys,
I presently have a program where I can load values from a file using fscanf() which is working fine.
FILE *fp = fopen("level.h", "r");
if (fp == NULL)
{
std::cout << "Error opening level.h\r\n";
return;
}
int s, w;
fscanf(fp, "%i", &numSect);
for (s = 0; s < numSect; s++)
{
fscanf(fp, "%i", &S[s].ws);
fscanf(fp, "%i", &S[s].we);
fscanf(fp, "%i", &S[s].z1);
fscanf(fp, "%i", &S[s].z2);
fscanf(fp, "%i", &S[s].st);
fscanf(fp, "%i", &S[s].ss);
}
I am trying to now have a near identical function which can do the same thing but from a string (or even a char array), by using sscanf.
But for some reason, it always return the first value of the string (being “1”). It's not stepping through the rest of the numbers like fscanf does.
std::string lvl = "1 0 4 0 40 1 4 4 192 288 256 288 0 1 1 0 256 288 256 352 0 1 1 90 256 352 192 352 0 1 1 0 192 352 192 288 0 1 1 90 288 48 30 0 0";
sscanf(lvl.c_str(), "%d", &numSect);
std::cout << "Sectors: " << numSect << "\r\n";
for (int s = 0; s < numSect; s++)
{
sscanf(lvl.c_str(), "%d", &S[s].ws); std::cout << "WS: " << S[s].ws << "\r\n";
sscanf(lvl.c_str(), "%d", &S[s].we); std::cout << "WE: " << S[s].we << "\r\n";
sscanf(lvl.c_str(), "%d", &S[s].z1); std::cout << "Z2: " << S[s].z1 << "\r\n";
sscanf(lvl.c_str(), "%d", &S[s].z2); std::cout << "Z2: " << S[s].z2 << "\r\n";
sscanf(lvl.c_str(), "%d", &S[s].st); std::cout << "ST: " << S[s].st << "\r\n";
sscanf(lvl.c_str(), "%d", &S[s].ss); std::cout << "SS: " << S[s].ss << "\r\n";
}
Is there a parameter I am missing for sscanf() or something?
I have done a search on the internet and can't see where I am going wrong.
Any advice would be greatly appreciated.