Mail Archives: djgpp/1997/10/17/22:31:12
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 -