Jump to content
  • Advertisement
Sign in to follow this  
donguow

sprintf and sprintf_s

This topic is 2495 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi guys,

I'm trying to send a sequence of floats over the network and here are several steps that I am following:

  1. Convert a float to a string (or char*)
  2. send the string to the client (using socket
  3. Repeat (1), (2) till the last float

I'm stuck at step (1): converting an float to a string. I'm using sprintf to do that (I've also tried with sprintf_s) but it doesn't work.


float bufferData[] = {
5.81414, 5.49492, 3.87633,
6.58321, 4.59523, 4.4363,
6.54791, 4.60198, 4.43473,
5.81414, 5.49492, 3.87633,
//.........
//.........
6.54791, 4.60198, 4.43473,
6.01867, 4.9006, 4.2733,
5.81414, 5.49492, 3.87633,
};

int N = sizeof(bufferData)/sizeof(float);

for (int i = 0; i < N; i++)
{
sprintf(buf, "%f", bufferData);
//send buf to the client
}


Error msg:
[bquote]
Unhandled exception at 0x00412fd0 in hybrid-rendering.exe: 0xC0000005: Access violation reading location 0x00000000.
[/bquote]

I know that it's pretty unsafe when using sprintf as we don't know the size of data in advance. So is there any alternative ways to do that sort of things? if someone knows how to fix this, please share with me, I will really appreaciate your help.

Thanks,
Dong

Share this post


Link to post
Share on other sites
Advertisement
My temporary solution now is using itoa (convert an integer to a string) instead of sprintf.

Server:

int temp;
float a;

for (int i = 0; i < N; i++)
{
a = bufferData * 1000000;
temp = (int)a;
itoa(temp, buf, 10);
// ...
send(sClient, buf, BUF_SIZE, 0);
}


Client:

int temp = 0;
float a;

for (int i = 0; i < N; i++)
{
recv(sServer, buf, BUF_SIZE, 0);
temp = atoi(buf);
a = (float) temp / 1000000;
//...
}

Share this post


Link to post
Share on other sites

This is working for me.. after adding char buf[16]; above the for loop.. are you sure the problem is within that snippet of code you posted?

EDIT: Oh but MSVC did scream at me for truncating doubles to floats (i.e. you need to append the suffix f to all those values in bufferData. C can get a little funny sometimes with data types and formating functions..

Share this post


Link to post
Share on other sites
It works for me sometimes as well but not in this case. I aim to get data from a buffer and send it to a different machine.
Here are how I did:

  1. First, I try to get the data out of the buffer by doing: glBindBuffer(GL_ARRAY_BUFFER, vboid);
    float* bufferData = (float*) glMapBuffer(GL_ARRAY_BUFFER, GL_READ_WRITE);

  2. Then, I send this data to the client as I did above
  3. And finally, unmap the buffer glUnmapBuffer(GL_ARRAY_BUFFER);

Do you have any idea, Roadysix?

Share this post


Link to post
Share on other sites
You almost certainly shouldn't be reading data from OpenGL and writing it to the network. Give us a high level view of what you are doing, there is probably an easier way.

Share this post


Link to post
Share on other sites

You almost certainly shouldn't be reading data from OpenGL and writing it to the network.

I just want to test something regardless of whether it is a good idea or not :) so just give it a try

Give us a high level view of what you are doing, there is probably an easier way.

My purpose is to get the data out of the buffer (say a vertex buffer) and then transmitt it to another machine to be further processed.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!