473,396 Members | 1,775 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,396 software developers and data experts.

Weird behavior: fputc adds an extra character.

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", to write, lets say, 1000 bytes in binary. The
output contained 1022 bytes, even though fwrite told me (returned)
that it only wrote 1000 bytes.

After much debugging, I saw that every time there was a "0x0A" byte to
be written, a "0x0D" was automatically appended! Thus adding extra
bytes to the file. What is going on?

I tried a simple loop with fputc instead of fwrite, and the behavior
was the exact same.
Anyone experienced this sort of thing before?

Thanks,
--Shafik

Feb 6 '07 #1
2 2910
Shafik wrote:
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", to write, lets say, 1000 bytes in binary. The
output contained 1022 bytes, even though fwrite told me (returned)
that it only wrote 1000 bytes.

After much debugging, I saw that every time there was a "0x0A" byte to
be written, a "0x0D" was automatically appended! Thus adding extra
bytes to the file. What is going on?
'\n' is guaranteed to be a single character in standard C, but it is
not a single character in your operating system's text format, so
fputc e.a. need to translate it at runtime. To disable this, don't
open the file in text mode, open the file in binary mode using the
second argument of fopen(). See also <http://c-faq.com/stdio/
fopenbinary.html>.

Feb 6 '07 #2
On Feb 6, 12:03 pm, "Harald van Dijk" <true...@gmail.comwrote:
Shafik wrote:
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", to write, lets say, 1000 bytes in binary. The
output contained 1022 bytes, even though fwrite told me (returned)
that it only wrote 1000 bytes.
After much debugging, I saw that every time there was a "0x0A" byte to
be written, a "0x0D" was automatically appended! Thus adding extra
bytes to the file. What is going on?

'\n' is guaranteed to be a single character in standard C, but it is
not a single character in your operating system's text format, so
fputc e.a. need to translate it at runtime. To disable this, don't
open the file in text mode, open the file in binary mode using the
second argument of fopen(). See also <http://c-faq.com/stdio/
fopenbinary.html>.
Perfect, thanks.

Feb 6 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: LRW | last post by:
I manage our mySQL database through putty (SSH terminal client). And whenever I do a select * from the table that contains ENCODEd passwords, the funky characters do funky things with the display....
11
by: Les Paul | last post by:
I'm trying to design an HTML page that can edit itself. In essence, it's just like a Wiki page, but my own very simple version. It's a page full of plain old HTML content, and then at the bottom,...
2
by: Gary Baydo | last post by:
The following program illustrates a question I have about fscanf() behavior: #include <stdio.h> #include <stdlib.h> #include <string.h> int main(void) { FILE *testfile; char s1 =...
3
by: Holger (David) Wagner | last post by:
Hi all, we're currently developing an application in the .NET environment that needs to access a DLL implemented in Fortran. There is one procedure with about 17 parameters, most of them arrays....
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);
28
by: entfred | last post by:
I have the following line of html: &nbsp;&nbsp1234&nbsp;&nbsp;&nbsp;&nbsp;&nbspabc&nbsp;&nbsp;&nbspyow In Internet Explorer 6.0, the columns look ok using the above html: 1234 abcd ...
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....
28
by: v4vijayakumar | last post by:
#include <string> #include <iostream> using namespace std; int main() { string str; str.resize(5); str = 't';
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.