B
bofh1234
I am trying to write a function that returns a float. This is what the
function should do: It takes the socket number and a variable name.
The function looks up the variable in an array of structs stored on a
server. The string (of a float number) stored is returned. The
function works except that it does not return a float. It retreives
the string fine, but I don't know why it is not converting the string
to a float. What am I doing wrong?
Thanks,
Mike
float readFloatVar(const int sock, const char *varname)
{
int n, size; /* socket descriptor, read count */
char *rpack, temp[64];
float returnvar;
readdelpacket.requesttype=2;
strcpy(readdelpacket.varname,varname);
write(sock, &readdelpacket, sizeof(readdelpacket)); /*request varible
name */
rpack=(char*)(&rpacket);
size=sizeof(rpacket);
n=0;
while ((n=read(sock, (void*)rpack, size)) >0){
size-=n;
rpack+=n;
} /* read response from server */
if (n < 0)
errexit("client socket read failed: %s\n", strerror(errno));
if (rpacket.rescode < 0)
returnvar = rpacket.rescode; /*variable not found */
else {
strcpy(temp,rpacket.rvarval); /*rpacket.rvarval is a char[64] */
printf("stubs rpacket.rvarval %s \n", temp); /* shows proper value
22333.3324 */
printf("ftubs rpacket.rvarval %15.5f \n", atof(temp)); /* shows
0.00000 should show 22333.3324*/
returnvar = atof(temp);
}
return returnvar;
}
function should do: It takes the socket number and a variable name.
The function looks up the variable in an array of structs stored on a
server. The string (of a float number) stored is returned. The
function works except that it does not return a float. It retreives
the string fine, but I don't know why it is not converting the string
to a float. What am I doing wrong?
Thanks,
Mike
float readFloatVar(const int sock, const char *varname)
{
int n, size; /* socket descriptor, read count */
char *rpack, temp[64];
float returnvar;
readdelpacket.requesttype=2;
strcpy(readdelpacket.varname,varname);
write(sock, &readdelpacket, sizeof(readdelpacket)); /*request varible
name */
rpack=(char*)(&rpacket);
size=sizeof(rpacket);
n=0;
while ((n=read(sock, (void*)rpack, size)) >0){
size-=n;
rpack+=n;
} /* read response from server */
if (n < 0)
errexit("client socket read failed: %s\n", strerror(errno));
if (rpacket.rescode < 0)
returnvar = rpacket.rescode; /*variable not found */
else {
strcpy(temp,rpacket.rvarval); /*rpacket.rvarval is a char[64] */
printf("stubs rpacket.rvarval %s \n", temp); /* shows proper value
22333.3324 */
printf("ftubs rpacket.rvarval %15.5f \n", atof(temp)); /* shows
0.00000 should show 22333.3324*/
returnvar = atof(temp);
}
return returnvar;
}