ok, here is my problem.

I am using sprintf to write a formatted string. That works fine.
it looks like this:

sprintf(buffer, "%f,%f,%i,%f,%f", user.x, user.y, user.dir, user.xspeed, user.yspeed);

where user is a struct
x is a float
y is a float
dir is a short int
xspeed is a float
yspeed is a float

I am parsing the exact same string using sscanf. And I end up with some crazy values.

sscanf(buffer, "%f,%f,%i,%f,%f", &remote.x, &remote.y, &remote.dir, &remote.xspeed, &remote.yspeed);

remote is another member of the same struct as user
x is never right
y is never right
dir is the only one that works i assume it has to do with the fact that it is the only int
xspeed I haven't even tested
yspeed is the same deal as xspeed


Why does this happen? I see no reason for it to not work

Even if I use the correct specifier for a short in sscanf, I see expected results.

#include <stdio.h>

int main(void)
{
   char buffer[100];
   struct 
   {
      float x,y;
      short dir;
      float xspeed, yspeed;
   } user = {123.45,6.789,100,43.21,98.76}, remote;
   sprintf(buffer, "%f,%f,%i,%f,%f", user.x, user.y, 
           user.dir, user.xspeed, user.yspeed);
   puts(buffer);
   sscanf(buffer, "%f,%f,%hi,%f,%f", &remote.x, &remote.y, 
          &remote.dir, &remote.xspeed, &remote.yspeed);
   printf("%f,%f,%i,%f,%f", remote.x, remote.y, 
          remote.dir, remote.xspeed, remote.yspeed);
   return 0;
}

/* my output
123.449997,6.789000,100,43.209999,98.760002
123.449997,6.789000,100,43.209999,98.760002
*/

Post the code that shows how you have initialized the user struct + buffer and what are the crazy values?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.