By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,130 Members | 2,228 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,130 IT Pros & Developers. It's quick & easy.

problem with fputc

P: n/a
When I execute the following code

#include <stdio.h>
#include <stdlib.h>
int main(void)
{
FILE *bmp;
bmp = fopen("deneme.bmp","w");

fputc(10,bmp);
fputc(9,bmp);
fputc(15,bmp);
fputc(16,bmp);

fclose(bmp);
system("PAUSE");
return 0;
}

the content of the deneme.bmp will be the
0D 0A 09 0F 10
in hex.

There is a problem writing 10. Why does this happen? Same problem
occurs in 11 and 12 also.

Aug 23 '07 #1
Share this Question
Share on Google+
3 Replies


P: n/a
On Aug 23, 3:27 pm, "Dinçay Akçören" <din...@gmail.comwrote:
When I execute the following code

#include <stdio.h>
#include <stdlib.h>
int main(void)
{
FILE *bmp;
bmp = fopen("deneme.bmp","w");

fputc(10,bmp);
fputc(9,bmp);
fputc(15,bmp);
fputc(16,bmp);

fclose(bmp);
system("PAUSE");
return 0;

}

the content of the deneme.bmp will be the
0D 0A 09 0F 10
in hex.

There is a problem writing 10. Why does this happen? Same problem
occurs in 11 and 12 also.
I see no "problem writing 10" in the example above. What problem do
you think you have?
Aug 23 '07 #2

P: n/a
Dinçay Akçören wrote On 08/23/07 15:27,:
When I execute the following code

#include <stdio.h>
#include <stdlib.h>
int main(void)
{
FILE *bmp;
bmp = fopen("deneme.bmp","w");

fputc(10,bmp);
fputc(9,bmp);
fputc(15,bmp);
fputc(16,bmp);

fclose(bmp);
system("PAUSE");
return 0;
}

the content of the deneme.bmp will be the
0D 0A 09 0F 10
in hex.

There is a problem writing 10. Why does this happen? Same problem
occurs in 11 and 12 also.
Your platform probably uses the CR-LF character pair to mark
the end of a line of text. C's text streams use the '\n' character
to mark the end of a line, so the library functions must translate
between the two conventions. Each CR-LF pair in an input stream
appears as a single '\n' when a C program reads it, and each '\n'
written to an output stream sends a CR-LF pair to the destination.
The remaining small piece of the puzzle is that the '\n' character
has the numerical code 10 in several widely-used character sets.

These translations are only applied to text streams. To create
a binary stream that writes what you give it without translating,
specify "wb" instead of "w" in the fopen() call. (For input, use
"rb" instead of "r".)

Final note: The details of the translations vary from platform
to platform, because different machines indicate end-of-line in
different ways.

--
Er*********@sun.com

Aug 23 '07 #3

P: n/a
Dinçay Akçören wrote:
When I execute the following code
[which is at EOM]
the content of the deneme.bmp will be the
0D 0A 09 0F 10
in hex.
There is a problem writing 10. Why does this happen? Same problem
occurs in 11 and 12 also.
Please examine the following:

#include <stdio.h>
#include <stdlib.h>
#include <limits.h>

void AssignValuesAndClose(FILE * bmp)
{
fputc(10, bmp);
fputc(9, bmp);
fputc(15, bmp);
fputc(16, bmp);
fputc(11, bmp); /* added because of comment */
fputc(12, bmp); /* added because of comment */
fclose(bmp);
}
void ShowValues(char *s)
{
FILE *f;
int c;
printf("opening \"%s\" in \"rb\" mode.\n", s);
if (!(f = fopen(s, "rb"))) {
fputs("open failed, quitting.\n", stderr);
exit(EXIT_FAILURE);
}
printf("contents:\n");
while ((c = fgetc(f)) != EOF)
printf("%02x ", (unsigned) c);
printf("\n\n");
fclose(f);
remove(s);
}
int main(void)
{
FILE *bmp;
char *fname;

if (!(fname = tmpnam(0))) {
fputs("tmpnam() failed, quitting.\n", stderr);
exit(EXIT_FAILURE);
}

printf("opening \"%s\" in \"w\" mode.\n", fname);
if (!(bmp = fopen(fname, "w"))) {
fputs("open failed, quitting.\n", stderr);
exit(EXIT_FAILURE);
}
AssignValuesAndClose(bmp);
ShowValues(fname);
printf("opening \"%s\" in \"wb\" mode.\n", fname);
if (!(bmp = fopen(fname, "wb"))) {
fputs("open failed, quitting.\n", stderr);
exit(EXIT_FAILURE);
}
AssignValuesAndClose(bmp);
ShowValues(fname);
return 0;
}

[Output]
opening "\tmp/dj100000" in "w" mode.
opening "\tmp/dj100000" in "rb" mode.
contents:
0d 0a 09 0f 10 0b 0c

opening "\tmp/dj100000" in "wb" mode.
opening "\tmp/dj100000" in "rb" mode.
contents:
0a 09 0f 10 0b 0c
[EOM, OP's code]
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
FILE *bmp;
bmp = fopen("deneme.bmp","w");

fputc(10,bmp);
fputc(9,bmp);
fputc(15,bmp);
fputc(16,bmp);

fclose(bmp);
system("PAUSE");
return 0;
}
Aug 23 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.