News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

sprintf assistance

Started by mahell, November 24, 2010, 06:17:20 AM

Previous topic - Next topic

mahell

I have searched and googled for an example of how to convert this to masm format. Maybe on this forum someone can give me assistance?

//Temporarily store the user input into an array
sprintf(tempSQL, "INSERT INTO products VALUES(NULL ,'%s','%s','%s','%s','%s');",
  newProductParentID, newProductName,
  newProductQuantity, newProductSerial,
  newProductPrice);
             
//Create the correctly formatted mysql query string
char * querySQL = strtok(tempSQL,"\0");

donkey

The API version of sprintf is wsprintf, you will have to check to see what inc and lib you need since you're using MASM.

.data
    sqlformat db  "INSERT INTO products VALUES(NULL ,'%s','%s','%s','%s','%s')",0
.data?
    tempSQL db 256 DUP (?)

.code
invoke wsprintf, offset tempSQL, offset sqlformat, newProductParentID, newProductName, \
    newProductQuantity, newProductSerial, newProductPrice


For the strtok I am not sure but it should be in msvcrt.dll

Edgar
"Ahhh, what an awful dream. Ones and zeroes everywhere...[shudder] and I thought I saw a two." -- Bender
"It was just a dream, Bender. There's no such thing as two". -- Fry
-- Futurama

Donkey's Stable

evlncrn8

yep should be in msvcrt indeed.. also sprintf is depricated.. so use _snprintf etc.. or the other safe variants...
wsprintf still has the 1024kb max buffer size limit, where sprintf (and the others) don't appear to have this..

Vortex

Hi mahel,

Here is a strtok example :



.386
.model flat,stdcall
option casemap:none

include     \masm32\include\windows.inc
include     \masm32\include\kernel32.inc
include     \masm32\include\msvcrt.inc

includelib  \masm32\lib\kernel32.lib
includelib  \masm32\lib\msvcrt.lib


.data

mystr       db 'This is a strtok demo. Example coded with Masm.',0
format1     db '%s',13,10,0

delimiters  db ' .',0

.code

start:

;   The C run-time functions are prefixed with the tag crt_ to avoid conflicts
;   with reserved Masm keywords.

    invoke  crt_strtok,ADDR mystr,ADDR delimiters
@@:
    invoke  crt_printf,ADDR format1,eax
    invoke  crt_strtok,NULL,ADDR delimiters
    test    eax,eax
    jnz     @b

    invoke  ExitProcess,0

END start

donkey

Pretty sure that in this case strtok is not required, it is looking for the escape sequence for a null terminator (\0) and inserting a null terminator in its place, from what I understand about the function the usage above does nothing anyway.
"Ahhh, what an awful dream. Ones and zeroes everywhere...[shudder] and I thought I saw a two." -- Bender
"It was just a dream, Bender. There's no such thing as two". -- Fry
-- Futurama

Donkey's Stable

GregL

I realize it doesn't apply here, but the thing to remember about sprintf vs. wsprintf is that sprintf handles floating-point and wsprintf does not.

donkey

Quote from: GregL on November 25, 2010, 06:26:58 PM
I realize it doesn't apply here, but the thing to remember about sprintf vs. wsprintf is that sprintf handles floating-point and wsprintf does not.


That's correct, swprintf from the CRT is one I use from time to time (most of my software is Unicode). I generally prefer the API over CRT functions but these days I seem to be using the CRT functions more often, mainly for conversions and sorting (_ui64toa and qsort).
"Ahhh, what an awful dream. Ones and zeroes everywhere...[shudder] and I thought I saw a two." -- Bender
"It was just a dream, Bender. There's no such thing as two". -- Fry
-- Futurama

Donkey's Stable