
problem with different putchar()'s - please help
On Mon, 7 Jun 1999 01:44:49 -0500, "Philip Pesek"
Quote:
> I am writing a function which sends an raw gif image file through stdio
> using a loop of putchar() functions (not printf or puts becuase of the
> character handling they do) for a cgi program and when I compile it with a
> win32 compiler, putchar prints an 0x0D before each 0x0A which creates
> unwanted characters and destroys the file. Under SunOS, (which i dont want
> this program in), however, it seems to just put in the 0x0A like all the
> other characters. So far I havent been able to figure out how to print the
> file in tact to stdio in NT. The putchar() function seems to be the most
> basic for printing to the screen, is there anyway to bypass its features?
> Thanks,
> Phil "KINGCOBRA" Pesek
<Jack>
Your first mistake is thinking that putchar() and other <stdio.h>
functions output to a screen. They do not. They send their output to
streams. On some platforms and some operating systems such a stream
might happen to be connected to a driver for a display screen but
neither the C language nor the definition of putchar() require this to
be so. In your particular case you most certainly do not want the
output of putchar() sent to the Windows text mode console window, you
want it to go somewhere else completely.
By definition, all files in C are opened in text mode, unless
specifically opened in binary mode. If an operating environment uses
something other than a plain '\n' to indicate the end of lines in text
files the C standard requires that input and output from and to text
files translate between whatever the operating system uses and a plain
'\n'.
So standard C requires that when you output '\n' to a text mode stream
the DOS/Windows end of line sequence "\r\n" is produced.
The solution? Open a file in binary mode and output to it, or use
freopen() to reopen stdout in binary mode. Note that this might not
be possible unless the operating system supplies a file name to do
this with.
There might also be a non-standard, compiler specific function to
change the mode of stdout to binary with you build your program or
once it begins execution. This would not be part of standard C and is
beyond the scope of this newsgroup.
Look up console functions in your online help or ask in a Windows
programming newsgroup or one which supports your particular brand of
compiler.
</Jack>
--
Do not email me with questions about programming.
Post them to the appropriate newsgroup.
Followups to my posts are welcome.