Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 404
  • Last Modified:

Splitting string using sscanf

I am trying to parse the following string:-  PPMVYPlacement:2098,305,2820,825,44,0,1
I want to store only the integer values in the appropriate varibles (w,x,y,z, wx,yz,za).

I used a sscanf to


//i initialized all the variables to 0.

sscanf((*buffer+15),_T("%d,%d,%d,%d,%u,%u,%u"), &w,&x,&y,&z,&wx,&yz,&za);

the problem is :- after i run the sscanf function i am getting all the variables as 0 and not the values that are supposed to be in them {2098, 305....}.

please attach a snippet on how to solve this problem, where i went wrong?

thanks
0
hari4130
Asked:
hari4130
  • 4
2 Solutions
 
AxterCommented:
Hi hari4130,
>> *buffer+15
What is buffer?
It doesn't look like you're correctly passing in the buffer.

David Maisonave :-)
Cheers!
0
 
AxterCommented:
sscanf can not take a wide character for the second argument, so that should not have a _T macro on it.
0
 
AxterCommented:
You should have something like the following:

sscanf(buffer+15,"%d,%d,%d,%d,%u,%u,%u", &w,&x,&y,&z,&wx,&yz,&za);

The above should work IF buffer is a char* type.
0
 
AxterCommented:
If you're still having problems, then post the buffer declaration.
0
 
nabehsCommented:
try the following code:

CString strBuffer = "PPMVYPlacement:2098,305,2820,825,44,0,1";
CString strData = strBuffer.Mid(15);
int w, x, y, z, wx, yz, za;
int begin = 0;
int pos = strData.Find(',', begin);
if(pos > -1)
{
      w = atoi(LPCTSTR(strData.Mid(begin, pos)));
      begin = pos + 1;
      pos = strData.Find(',', begin);

      x = atoi(LPCTSTR(strData.Mid(begin, pos)) );
      begin = pos + 1;
      pos = strData.Find(',', begin);

      y = atoi(LPCTSTR(strData.Mid(begin, pos)));
      begin = pos + 1;
      pos = strData.Find(',', begin);

      z = atoi(LPCTSTR(strData.Mid(begin, pos)));
      begin = pos + 1;
      pos = strData.Find(',', begin);

      wx = atoi(LPCTSTR(strData.Mid(begin, pos)));
      begin = pos + 1;
      pos = strData.Find(',', begin);

      yz = atoi(LPCTSTR(strData.Mid(begin, pos)));
      begin = pos + 1;
      pos = strData.Find(',', begin);

      za = atoi(LPCTSTR(strData.Mid(begin)));
}
0

Featured Post

[Webinar] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now