problem with fputc

  • Thread starter =?iso-8859-9?B?RGlu52F5IEFr5/ZyZW4=?=
  • Start date
?

=?iso-8859-9?B?RGlu52F5IEFr5/ZyZW4=?=

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.
 
H

Harald van =?UTF-8?B?RMSzaw==?=

Dinçay Akçören said:
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.

You need to open the file in binary mode. In short, 10 happens to be equal
to '\n' on your system, and '\n' gets special treatment in text files on
your system. See the documentation for fopen, or the FAQ at
<http://c-faq.com/> (in particular, question 12.40) for details.
 
L

Lew Pitcher

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?
 
E

Eric Sosman

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.
 
M

Martin Ambuhl

Dinçay Akçören said:
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]
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top