🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.sys.mac
Article #24565

Re: sprintf() bug?

#24565
From: Gray Watson
Date: Mon, 01 Feb 1988 18:04
39 lines
2286 bytes
>  sprintf(tempstring, "%s %f %s %f", "\pthe square of ", x, "is", (x *x));
>  DrawString(tempstring);

Article <5349@cit-vax.Caltech.Edu> stated that the lines above "will not work
in any C which [he] understand[s]".  Tempstring should, according the article
be equal to "\016the square of" followed by random garbage.

Well I don't know what version of C you are using but the example WILL work:

	Sscanf should, like strcat, strcpy, etc. pad any string produced with
with a \0 (NULL) character.  If it doesn't then it is your compiler's fault.
	The \p (which for non-Mac routines does insert a \016) is needed for
the Mac interface.  The first character in the string you send to DrawString
should be the value of the length of the string or should be \p or \016:

If you are setting up a window title to be "Hello Dolly" the string you
	should give the OpenWindow call (or whatever it is) should be
	"\011Hello Dolly" with \011 being the length of "Hello Dolly",
	but "\pHello Dolly" saves you from recounting the length of the string
	if you change it a lot.


	Also when any of the Mac's ROM based file routines return a file name
string it doesn't have a \0 (NULL) at the end of the char array but instead
the value of first character is the length of the string.

To translate from a Mac returned string into correct C format:

If file_name[20] is returned I do a:
	file_name[file_name[0]] = 0; /* punch the string-end with a null */
	and refer to the string as &file_name[1]

Or if you have memory and speed to burn:
	If temp[20] is returned:	
	You can do a strncpy(file_name,&temp[1],temp[0]);
	Which makes file_name be a correct C string with the returned name.


Have fun...
Technical Details
Message-ID: <943@cadre.dsl.PITTSBURGH.EDU>
Path: arc1-ca.pugleaf.net!utzoo!mnetor!uunet!lll-winken!lll-lcc!lll-tis!mordor!sri-spam!rutgers!rochester!PT.CS.CMU.EDU!cadre!cgw
References: <4VzXREy00WABE7k0FU@andrew.cmu.edu> <5349@cit-vax.Caltech.Edu>

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!utzoo!mnetor!uunet!lll-winken!lll-lcc!lll-tis!mordor!sri-spam!rutgers!rochester!PT.CS.CMU.EDU!cadre!cgw
From: cgw@cadre.dsl.PITTSBURGH.EDU (Gray Watson)
Newsgroups: comp.sys.mac
Subject: Re: sprintf() bug?
Message-ID: <943@cadre.dsl.PITTSBURGH.EDU>
Date: 1 Feb 88 18:04:14 GMT
References: <4VzXREy00WABE7k0FU@andrew.cmu.edu> <5349@cit-vax.Caltech.Edu>
Reply-To: cgw@cadre.dsl.pittsburgh.edu.UUCP (Gray Watson)
Organization: Decision Systems Lab., Univ. of Pittsburgh, PA.
Lines: 38
Keywords: C,sprintf,strings
Summary: Corection to article