Connecting Tech Pros Worldwide Help | Site Map

fputc problem

Newbie
 
Join Date: Sep 2009
Posts: 7
#1: 4 Weeks Ago
My program is an 'attempt' at steganography, and i've written what i hope to be correct for the most part, although i'm having a lot of trouble with copying the picture input to picture output verbatim.

while (bytes > 0)
{
int c = fgetc(fin);
fputc(c, fout);b
bytes--;
}

it's, quite obviously, a simple while loop that occurs after i've already copied the header and encoded a simple number (my input file length) into the previous 86 bytes. it is supposed to just copy the picture verbatim from that point
but for some reason every time i run it, on the 1105th (exactly) iteration, fputc seems to put two bytes into fout, and does so every time the 'c' byte value is 10 after that. i've checked it numerously with ftell(fout) and have no idea why it either skips one byte or decides to add two identical ones (haven't been able to figure out what it's doing).
i'm fairly positive that fputc can only add one byte at a time so any help here??
Expert
 
Join Date: Mar 2008
Location: Naperville, Illinois U.S.
Posts: 828
#2: 4 Weeks Ago

re: fputc problem


Quote:

Originally Posted by ocli5568 View Post

... for some reason every time i run it, on the 1105th (exactly) iteration, fputc seems to put two bytes into fout, and does so every time the 'c' byte value is 10 after that.

Character code 10 (0x0A, ASCII LF) is commonly used by C as the logical 'newline' character, and is used to delimit lines of text.

By logical newline I mean the character you use in your program; the physical newline is the character(s) actually present in the text file. Different operating systems have different conventions for physical newline. For example, here are the physical newline codes for some common operating systems:
  • Windows: the 2-byte sequence 0x0D, 0x0A
  • Macintosh: the single byte 0x0D
  • UNIX: the single byte 0x0A
The C compiler automatically translates between logical and physical newline for text files for you. It does this for both input and output.

Try opening the input and output files as binary files (refer to the fopen documentation).

By the way, you ought to be trapping error returns from fgetc and fputc. You never know when something might go wrong.
Newbie
 
Join Date: Sep 2009
Posts: 7
#3: 4 Weeks Ago

re: fputc problem


thank you so much, i accidentally opened the bmp as fopen("<name>", "w") instead of ("<name>", "wb"), that was just killing me!
but what do you mean by trapping error returns? implementing
assert(c != EOF); after fgetc?
Expert
 
Join Date: Mar 2008
Location: Naperville, Illinois U.S.
Posts: 828
#4: 3 Weeks Ago

re: fputc problem


The documentation for fgetc and fputc describes how these functions report failure to the caller. If you call these functions then you should check for those failure conditions.
Reply

Tags
fputc, skip byte, strange problem, while loop