473,320 Members | 2,111 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

fputc problem

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??
Oct 24 '09 #1
3 4598
donbock
2,426 Expert 2GB
@ocli5568
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.
Oct 24 '09 #2
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?
Oct 24 '09 #3
donbock
2,426 Expert 2GB
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.
Oct 26 '09 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

117
by: Peter Olcott | last post by:
www.halting-problem.com
145
by: Sidney Cadot | last post by:
Hi all, In a discussion with Tak-Shing Chan the question came up whether the as-if rule can cover I/O functions. Basically, he maintains it can, and I think it doesn't. Consider two...
28
by: Jon Davis | last post by:
If I have a class with a virtual method, and a child class that overrides the virtual method, and then I create an instance of the child class AS A base class... BaseClass bc = new ChildClass();...
6
by: Ammar | last post by:
Dear All, I'm facing a small problem. I have a portal web site, that contains articles, for each article, the end user can send a comment about the article. The problem is: I the comment length...
4
by: Summu82 | last post by:
Hi i have to write some data in a binary file fputc(0x22,f2); this works fine also int data =0x22; fputc(0x22,f2);
4
by: Putty | last post by:
I'm porting a program a friend wrote in C over to Python and I've run into a little hang-up. The C program writes characters out to a file. I'm 99% sure that a conversion is going on here as well....
3
by: Vivienne | last post by:
I am using VS 2005. In a project when I was trying to write some unsigned char into a file, I used fwrite() and fputc(). But I found whenever I tried to write 0x0a, fwrite() function automaticly...
2
by: Shafik | last post by:
Hello folks, I am having a really weird problem. Under windows XP, a simple C program designed to write to a file has been adding EXTRA characters to the output file. First I used "fwrite",...
3
by: =?iso-8859-9?B?RGlu52F5IEFr5/ZyZW4=?= | last post by:
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);
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.