The MASM Forum Archive 2004 to 2012

General Forums => The Campus => Topic started by: mahell on November 24, 2010, 06:17:20 AM

Title: sprintf assistance
Post by: mahell on November 24, 2010, 06:17:20 AM
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");
Title: Re: sprintf assistance
Post by: donkey on November 24, 2010, 06:42:41 AM
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
Title: Re: sprintf assistance
Post by: evlncrn8 on November 24, 2010, 09:52:48 AM
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..
Title: Re: sprintf assistance
Post by: Vortex on November 24, 2010, 06:48:41 PM
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
Title: Re: sprintf assistance
Post by: donkey on November 24, 2010, 07:10:59 PM
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.
Title: Re: sprintf assistance
Post by: 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.
Title: Re: sprintf assistance
Post by: donkey on November 25, 2010, 11:19:18 PM
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).