473,396 Members | 1,770 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.

I met a wierd thing when using fwrite() and fputc(), 0x0d is automatically added before 0x0a

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 wrote 0x0d before 0x0a. fputc()
gives the same results. So I wrote a small test to test it. Here is it:
FILE* file;
unsigned char p = 0x0a;
long offset = ftell(file); // here the offset = 0;
fwrite(*p,1,1,file);

offset = ftell(file); // after this, the offset = 2. and
in the file I

// found 0x0d and 0x0a was
written.

I can't understand. I just want to write byte by byte, automatically
inserted byte 0x0d will cause problems in my project.

can someone tell me what is the cause and how to avoid the unwanted
byte?

Jan 14 '07 #1
3 4075
Vivienne wrote, On 14.1.2007 15:14:
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 wrote 0x0d before 0x0a. fputc()
gives the same results. So I wrote a small test to test it. Here is it:
FILE* file;
unsigned char p = 0x0a;
long offset = ftell(file); // here the offset = 0;
fwrite(*p,1,1,file);

offset = ftell(file); // after this, the offset = 2. and
in the file I

// found 0x0d and 0x0a was
written.

I can't understand. I just want to write byte by byte, automatically
inserted byte 0x0d will cause problems in my project.

can someone tell me what is the cause and how to avoid the unwanted
byte?
You are opening your file in text mode, and you are on Windows. Make sure you
open your files with "b" mode if you do not want the semantics of text streams.

--
VH
Jan 14 '07 #2

"Vivienne" <zh**********@gmail.comwrote in message
news:11**********************@51g2000cwl.googlegro ups.com...
Here is it:
>

FILE* file;
unsigned char p = 0x0a;
long offset = ftell(file); // here the offset = 0;
If this is the code, you are accessing an uninitialized pointer "file".
Nowhere do you set "file" to point somewhere.

Read the FAQ on posting these type of questions:
http://www.parashift.com/c++-faq-lit...t.html#faq-5.8

So, is this your real code, or did you open the file somehow? If you did
open the file, did you open it as binary or text? If you opened it as text,
that is more than likely the reason for your problem.

Read up on fopen( ) and the difference in opening a file as binary or text.
The hint is the second parameter to fopen.

Paul
Jan 14 '07 #3
This is not the real code, I just copied some lines here.
now I have got the answer, I should have opened the file in banery
mode, instead text mode.
Thank you very much!

"Paul дµÀ£º
"
"Vivienne" <zh**********@gmail.comwrote in message
news:11**********************@51g2000cwl.googlegro ups.com...
Here is it:


FILE* file;
unsigned char p = 0x0a;
long offset = ftell(file); // here the offset = 0;

If this is the code, you are accessing an uninitialized pointer "file".
Nowhere do you set "file" to point somewhere.

Read the FAQ on posting these type of questions:
http://www.parashift.com/c++-faq-lit...t.html#faq-5.8

So, is this your real code, or did you open the file somehow? If you did
open the file, did you open it as binary or text? If you opened it as text,
that is more than likely the reason for your problem.

Read up on fopen( ) and the difference in opening a file as binary or text.
The hint is the second parameter to fopen.

Paul
Jan 15 '07 #4

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

Similar topics

8
by: Paul Watson | last post by:
Can a for loop be used in a one-liner? What am I missing? $ python -c "import sys;print ''.join()," now is the time now is the time $ python -c "import sys;for line in...
7
by: Yandos | last post by:
Hello all, I have maybe a trivial question, but I cannot think out what is wrong :( How do i detect EOF correctly when i read from stdin? Am I doing it wrong? <pipetest.c> #include <stdio.h>...
112
by: Tom | last post by:
This is very strange: I have a Windows Form with a Panel on it. In that panel I dynamically (at run time) create some labels, as so: for i=1 to x dim ctlNew as New Label() with ctlNew...
3
by: Tom | last post by:
We are experiencing some wierd debugging behavior. What happens is that, during debugging with VS 2003, the debugger seems to 'skip' statements that are associated with database operations. For...
30
by: empriser | last post by:
How to use fread/fwrite copy a file. When reach file's end, fread return 0, I don't konw how many bytes in buf.
3
by: leow88 | last post by:
I want to read/write to/from a binary file. For an existing file FILE *pFile = fopen ("MyFile.bin","rb+" ) would be correct. But the moment I write a byte with value 0x0D to the file a 0x0A is...
5
by: sweeet_addiction16 | last post by:
hello experts...pls help me debug this code........im not able to write in the data bytes ..only the few header bytes are written... #include<stdio.h> struct mthd_chunk { char id;...
9
Death Slaught
by: Death Slaught | last post by:
I'm working on a web site, and every thing is fine except when I added the navagation bar......for some reason it pushes my navagation bar away from the border, which is odd because there's no reason...
12
by: Medvedev | last post by:
FILE *a,*b; int z; char str1; char str2; a= fopen(argv,"rb"); //Both files are EXE's b= fopen(argv,"rb+"); z= fread(str1,1,1,a); if (z !=1)
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
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...
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
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.