delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1997/10/17/22:31:12

From: mert0407 AT sable DOT ox DOT ac DOT uk (George Foot)
Newsgroups: comp.os.msdos.djgpp
Subject: Re: sprintf() string length?
Date: 17 Oct 1997 23:59:54 GMT
Organization: Oxford University, England
Message-ID: <628u5q$bki$1@news.ox.ac.uk>
References: <3 DOT 0 DOT 16 DOT 19971016132047 DOT 34a756c4 AT hem1 DOT passagen DOT se>
NNTP-Posting-Host: sable.ox.ac.uk
Lines: 28
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

On Thu, 16 Oct 1997 17:26:49 GMT in comp.os.msdos.djgpp Peter Palotas
(blizzar AT hem1 DOT passagen DOT se) wrote: 

: Does anyone know if this works, and how compatible it is?
: Writing to a NULL pointer doesn't sound too good to me!

The technique isn't to write to a NULL pointer; it's to assign a FILE * to
the filename "NUL" for DOS, or more portably "/dev/nul". Like so:

FILE *fp = fopen ("/dev/nul","wt");

Then you simply fprintf to that file and check the return value:

numchars = fprintf (fp,"Testing%s%c%d",some_string,some_char,some_number);
fclose(fp);

Finally you can pass `numchars' to malloc:

str = (char *) malloc (numchars);
sprintf (str,"Testing%s%c%d");

One thing I'm not sure of is whether the character count includes expansions
of LF -> CRLF (or is it CR -> CRLF?), which a text file on a DOS system would
do automatically.

-- 
George Foot <mert0407 AT sable DOT ox DOT ac DOT uk>
Merton College, Oxford

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019