I am learning how to write a simple CGI page with C language. I tried with Apache on both Linux and Windows. I compiled my scripts on 2 different computers that run different OSes.
- Firstly, I created a simple CGI page for getting a static plain-text content:
#include int main() { FILE *fp = fopen("plain_text.txt", "r"); // text-mode only. if (fp) { int ch; printf("content-type: text/plain\n\n"); while ((ch = fgetc(fp)) != EOF) { printf("%c", ch); } fclose(fp); } return 0; }
I compiled it into an executable and put it in cgi-bin
directory. When I browse it with my web-browser, it returns the plain-text content correctly (both Linux and Windows).
- Then, I modified above script for getting a simple JPEG content. (I understand that: every JPEG picture is a binary file)
#include int main() { FILE *fp = fopen("cat_original.jpg", "rb"); // with binary-mode. if (fp) { int ch; printf("content-type: image/jpg\n\n"); while (((ch = fgetc(fp)) != EOF) || (!feof(f1))) // can read whole content of any binary file. { printf("%c", ch); } fclose(fp); } return 0; }
I compiled it into an executable and put it in cgi-bin
directory, too.
I can get the correct returned-image with Linux compiled-executable files; but, the Windows does not.
To understand the problem, I downloaded the returned-image with Windows compiled-execute files.
(I named this image: cat_downloaded_windows.jpg
)
Then, I used VBinDiff for compare 2 images: cat_original.jpg
(68,603 bytes) and cat_downloaded_windows.jpg
(68,871 bytes).
There are many lines in cat_downloaded_windows.jpg
(like the row I marked) have a character which cat_original.jpg
does not have.
So, I guess that the Windows OS causes the problem (Windows add some characters automatically, and Linux does not) (Apache and web-browsers do not cause problem)
So, I posted this topic into StackOverflow for getting your helps. I have 2 questions:
- Is there any problem with the
printf("%c", ch);
(in my script) on Windows? - Is there any way to print binary content into
stdout
, both Linux and Windows?
I am learning programming myself, and this is the first time I ask on StakOverflow. So, if my question is not clear, please comment below this question; I will try to explain it more.
Thank you for your time!