473,666 Members | 2,038 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2935
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.htm l>.

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.htm l>.
Perfect, thanks.

Feb 6 '07 #3

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

Similar topics

0
1429
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. From doing weird alignment things, to making it so that so long as those characters are visible, the display is always scrolled to a line where one of those screwy ASCII characters are. I can scroll up or down, but as soon as I type something on...
11
13687
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, there's an "Edit" link. So the page itself looks something like this: <HTML><HEAD><TITLE>blah</TITLE></HEAD><BODY> <!-- TEXT STARTS HERE --> <H1>Hello World!</H1> <P>More stuff here...</P>
2
1541
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 = "01234567890123456789";
3
5818
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. After some trial and error experiments, we've found out that a) the parameters must be ordered by type, i.e. we cannot mix double, integers and Strings (char-Arrays) - instead, we first need to put all the float-Arrays and Int32-Arrays, and...
4
4324
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
13185
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 yow But, in firefox, for same html, getting this:
4
3170
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. I know for a fact that it's taking a number and turning it into a character. So what kind of call can I make to do it in Python?
28
2532
by: v4vijayakumar | last post by:
#include <string> #include <iostream> using namespace std; int main() { string str; str.resize(5); str = 't';
3
4311
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
8444
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8356
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8869
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8781
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8551
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8639
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7386
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4198
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2771
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.