473,624 Members | 2,439 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

writing 0x0A to a bin file without having it automatically write 0x0D

This is probably a common problem, but as a novice programmer I'm
trying to figure out how to write 0x0A to a BIN file without having it
automatically write 0x0D. Here's an example of the code:
tmp_ifp = fopen("tmp_name .bin", "w");
fprintf(tmp_ifp , "%c", 0x0A);

When I open the tmp_name.bin file with a hex editor you notice that
both 0x0D and 0x0A are written. I tried using fwrite, but the same
thing occurs. Any ideas?

Thanks....

Mar 13 '06 #1
15 6290
MC*******@gmail .com writes:
This is probably a common problem, but as a novice programmer I'm
trying to figure out how to write 0x0A to a BIN file without having it
automatically write 0x0D. Here's an example of the code:
tmp_ifp = fopen("tmp_name .bin", "w");
fprintf(tmp_ifp , "%c", 0x0A);

When I open the tmp_name.bin file with a hex editor you notice that
both 0x0D and 0x0A are written. I tried using fwrite, but the same
thing occurs. Any ideas?


You need to open the file in "binary" mode, e.g.

tmp_ifp = fopen("tmp_name .bin", "wb");
--

John Devereux
Mar 13 '06 #2
MC*******@gmail .com writes:
This is probably a common problem, but as a novice programmer I'm
trying to figure out how to write 0x0A to a BIN file without having it
automatically write 0x0D. Here's an example of the code:
tmp_ifp = fopen("tmp_name .bin", "w");


Use binary mode: "wb" instead of "w".
--
"When in doubt, treat ``feature'' as a pejorative.
(Think of a hundred-bladed Swiss army knife.)"
--Kernighan and Plauger, _Software Tools_
Mar 13 '06 #3
On 2006-03-13, MC*******@gmail .com <MC*******@gmai l.com> wrote:
This is probably a common problem, but as a novice programmer I'm
trying to figure out how to write 0x0A to a BIN file without having it
automatically write 0x0D. Here's an example of the code:
tmp_ifp = fopen("tmp_name .bin", "w");
fprintf(tmp_ifp , "%c", 0x0A);

When I open the tmp_name.bin file with a hex editor you notice that
both 0x0D and 0x0A are written. I tried using fwrite, but the same
thing occurs. Any ideas?

Thanks....


I think these routines can convert 0xa to a system defined
line terminator : which I guess in your case is 0xD,0xA.

Check the options which can follow "w" in your fopen : b or t might
help if memory serves correctly. Since you talk about "bin" file then
"wb" might be your man.
Mar 13 '06 #4
On 2006-03-13, MC*******@gmail .com <MC*******@gmai l.com> wrote:
This is probably a common problem, but as a novice programmer I'm
trying to figure out how to write 0x0A to a BIN file without having it
automatically write 0x0D. Here's an example of the code:
tmp_ifp = fopen("tmp_name .bin", "w");
"wb".
fprintf(tmp_ifp , "%c", 0x0A);

When I open the tmp_name.bin file with a hex editor you notice that
both 0x0D and 0x0A are written. I tried using fwrite, but the same
thing occurs. Any ideas?

Thanks....

Mar 13 '06 #5
On 2006-03-13, Richard G. Riley <rg****@gmail.c om> wrote:
On 2006-03-13, MC*******@gmail .com <MC*******@gmai l.com> wrote:
This is probably a common problem, but as a novice programmer I'm
trying to figure out how to write 0x0A to a BIN file without having it
automatically write 0x0D. Here's an example of the code:
tmp_ifp = fopen("tmp_name .bin", "w");
fprintf(tmp_ifp , "%c", 0x0A);

When I open the tmp_name.bin file with a hex editor you notice that
both 0x0D and 0x0A are written. I tried using fwrite, but the same
thing occurs. Any ideas?

Thanks....


I think these routines can convert 0xa to a system defined
line terminator : which I guess in your case is 0xD,0xA.

Check the options which can follow "w" in your fopen : b or t might
help if memory serves correctly. Since you talk about "bin" file then
"wb" might be your man.


t is not standard C<OT>, and where it is used, it means text mode</OT>.
Mar 13 '06 #6
On 13 Mar 2006 14:03:44 -0800, MC*******@gmail .com wrote:
This is probably a common problem, but as a novice programmer I'm
trying to figure out how to write 0x0A to a BIN file without having it
automaticall y write 0x0D. Here's an example of the code:
tmp_ifp = fopen("tmp_name .bin", "w");
fprintf(tmp_if p, "%c", 0x0A);

When I open the tmp_name.bin file with a hex editor you notice that
both 0x0D and 0x0A are written. I tried using fwrite, but the same
thing occurs. Any ideas?


You opened the file in text mode. Therefore, it is not a binary file,
regardless of what the three character extension is. If you want to
process the file in binary mode, open it binary mode.

For additional insurance, don't use fprintf. Use fwrite.
Remove del for email
Mar 14 '06 #7
In article <bd************ *************** *****@4ax.com>,
Barry Schwarz <sc******@doezl .net> wrote:
For additional insurance, don't use fprintf. Use fwrite.


Huh? How can these be different?

-- Richard
Mar 14 '06 #8
Thanks....I opened it with "wb" and it worked. I'm surprised that just
using "w" wouldn't. Why would you want to act on the data being
written to a file in that manner? Doesn't make sense. If someone
wanted a new line then they would just put "\n".
Thanks everyone else for your help too!

Mar 14 '06 #9
MikeC_EE99 wrote:
Thanks....I opened it with "wb" and it worked. I'm surprised that just
using "w" wouldn't. Why would you want to act on the data being
written to a file in that manner?
So that you write the correct data.
Doesn't make sense.
Does.
If someone wanted a new line then they would just put "\n".


But in a /file/, on some OSs incl Windows, a new line is represented
as \r\n, not just \n. So, for /text streams/, the C library converts
\r\n on input to \n, and \n on output to \r.

If you don't want that to happen, you're not writing a text stream,
so you have to tell C that you want a "binary" file.

--
Chris "sparqling" Dollin
"Who do you serve, and who do you trust?"
Mar 14 '06 #10

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

Similar topics

6
2966
by: Jean-Gael GRICOURT | last post by:
I am trying to use ostringstream in binary mode. Why the value (0x0A) is automatically replaced by (0x0D,0x0A) in the output ? Is there a workaround to this issue ? -------- my snippet code ------- #include <iostream> #include <sstream> using namespace std;
4
1506
by: phantom | last post by:
Hi All. I am having a problem writing to a file. I can successfully open the file and write the contents using fprintf, however if the writing is not done for a while in the process the file write returns a status -1. To elaborate here is the code segment main (... , ...) {
2
6849
by: melanieab | last post by:
Hi, I'm trying to store all of my data into one file (there're about 140 things to keep track of). I have no problem reading a specific string from the array file, but I wasn't sure how to replace just one item. I know I can get the entire array, then save the whole thing (with a for loop and if statements so that the changed data will be saved), but it seems like a lot of unnecessary reading and writing. Is there a way to directly save...
6
4267
by: dast | last post by:
Hello, I’m having trouble writing the contents of a dataset to a csv file. I have made a solution that loops through the rows in the dataset, but it’s too slow. There must be a faster and easier way. Here is a stripped down version of my failed attempt of writing without looping through the dataset:
12
3752
by: Chris Springer | last post by:
I'd like to get some feedback on the issue of storing data out to disk and where to store it. I've never been in a production environment in programming so you'll have to bear with me... My question is about storing data in a database. Yes I understand that you can link to a database in your program and read and write to the database etc etc. Well, that's all find and dandy but what if the person you're writing the application for...
1
3722
by: lactaseman | last post by:
While I know this is not the correct venue... I realize this is of little to no importance to most out there... however, if I had found this in my initial searches, I would have used this. So, as an alternative to the mentalis.org's IniReader, I submit the following files to the web news group DBs...: @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
6
2169
by: bonk | last post by:
I am trying to create a stream that writes text to a file and: - automatically creates a new file once the current file exceeds a certain size - makes it possible to be used by multiple threads and/or processes so that multiple threads/processes can write to the same file (all threads use the same instance of the stream, processes use a different instance but still may point to the same file) Could you point me in the correct...
3
4096
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 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;
2
4406
by: MAx | last post by:
Hi guys, I am a c++ newbee and i am trying to write a file to a default printer. Please have a look at the code below and let me know if it'll work. I know that similar code in C will work. I was wondering how will the associated driver react..... #include <iostream> #include <fstream>
0
8231
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
8168
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
8471
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
7153
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
4075
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...
0
4167
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2603
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
1
1780
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1474
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.