0

I need to do this in two separate steps but so far I am not finding a way of doing this.

First, I need to convert a double variable, into a char variable (and to be saved in that variable). I have noticed type casting doesnt work the same in C as Java / other languages. How do I cast a variable to be a string / char?

Second, I need to concatenate the strings, there will be a total of 6 string variables that will need concatenating, I have only found the strcat function which only takes 2 arguments.

These are the strings I am trying to build:

char *queryOne = "INSERT INTO location (id, carid, ownerid, lat, long, speed) VALUES (,2, 1, ";
char *queryTwo = lat; // lat is a double
char *queryThree = ",";
char *queryFour = longatude; // longatude is a double
char *queryFive = ",";
char *querySix = speed; // speed is a double

And then I need the concatenated string to work in: (mysql_query(conn, query)) as one long string

Edit: So possibly, this should convert the datatype I think?

char buffer [50];

char *queryOne = "INSERT INTO location (id, carid, ownerid, lat, long, speed) VALUES (,2, 1, ";
char *queryTwo = sprintf (buffer, "%d", lat);
char *queryThree = ",";
char *queryFour = sprintf (buffer, "%d", longatude);
char *queryFive = ",";
char *querySix = sprintf (buffer, "%d", speed);

fprintf(stderr, "Dta: %s\n", queryOne);
fprintf(stderr, "Dta: %s\n", *queryTwo);
fprintf(stderr, "Dta: %s\n", queryThree);
fprintf(stderr, "Dta: %s\n", *queryFour);
fprintf(stderr, "Dta: %s\n", queryFive);
fprintf(stderr, "Dta: %s\n", *querySix);
8
  • 1
    Look at sprintf. Commented Aug 5, 2015 at 19:27
  • Why do you need to do in two separate steps? Commented Aug 5, 2015 at 19:29
  • @JoeFarrell thanks, I have edited my post to use sprintf Commented Aug 5, 2015 at 19:31
  • @WeatherVane because I need to know how to convert to char from double, as I need one long string of chars to use with the SQL query Commented Aug 5, 2015 at 19:31
  • 1
    Does c not support query parameters? Commented Aug 5, 2015 at 19:32

1 Answer 1

1

In your case, you could use:

#define MAXSQL 256
char sql[MAXSQL];
snprintf(sql, MAXSQL,  "%s %f , %f , %f", queryOne, lat, longatude, speed);

The snprintf function writes onto the buffer, that is its first argument. http://www.cplusplus.com/reference/cstdio/snprintf/?kw=snprintf

Now you can use the sql string as you please.

Note that I used snprintf rather than sprintf. This is to avoid potential buffer overflows.

Also, don't use strcat so repeatedly, because that causes a Shlemiel the Painter algorithm, and every next call to strcat gets slower, because strcat has to start from the beginning and find the null terminator. See http://www.joelonsoftware.com/articles/fog0000000319.html for more info.

Sign up to request clarification or add additional context in comments.

5 Comments

Don't use %lf for doubles in printf family. Use it for doubles in scanf family. Here %f is sufficient for double.
Changed as you said @WeatherVane
@cpp_prog thanks for the suggestion, is this what you mean? mysql_query(conn, snprintf(sql, MAXSQL, "%s %f , %f , %f", queryOne, lat, longatude, speed)) as it is producing a segfault
No, you are passing the return value from snprintf which is its length. Make the string sql as advised, then mysql_query(conn, sql). I respectfully advise to read the manual.
@WeatherVane ah sorry I didn't realise sql was to be used as a variable, I thought that was just a buffer size

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.