473,320 Members | 2,104 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.

Writing 10

Hi

I'm having a wierd problem and I was wondering if anyone else had
encountered something similar...

I have a program which reads in a 5 digit number from a file in
decimal format, converts it to an unsigned int and writes it back out
to another file... It works fine in most cases, but for some reason
for the number 10 it doesn't.

It's Windows XP using Borland C++ and the code is something like...

char cp_decimal_num[5];
int i_bytes_read=read(i_in_file_id, cp_decimal_num, 5);
if (i_bytes_read != 5)
perror ("Error reading");

unsigned int ui_output_num = atoi(cp_decimal_num);

int i_bytes_written=write(i_out_file_id, &ui_output_num, 2);
if (i_bytes_written != 2)
perror ("Error writing");

Now in most cases it reads in the five digit number, (e.g. "00100"),
converts it to an unsigned int (100), and then writes 2 bytes to the
output file (64, 00).

This works for any number except... for some reason if I try it with
the number 10, it doesn't work as expected. It reads in "00010",
converts it to 10 (I've checked this in debug), but when writes it,
where I was expecting to see...
0A 00
....there is actually 3 bytes...
0D 0A 00

Yet the i_bytes_written variable still claims to have written only 2
bytes.

Can anyone tell me what's going on here? Where's that extra 0D coming
from?

Thanks
Colm
Jul 22 '05 #1
12 1579
Colm wrote:


char cp_decimal_num[5];
char cp_decimal_num[6];

***

You need room for the terminating '\0' character, which atoi
expects to be there.
cp_decimal_num[5] = '\0';
int i_bytes_read=read(i_in_file_id, cp_decimal_num, 5);
if (i_bytes_read != 5)
perror ("Error reading");

unsigned int ui_output_num = atoi(cp_decimal_num);
[snip]
0A 00
...there is actually 3 bytes...
0D 0A 00

Yet the i_bytes_written variable still claims to have written only 2
bytes.

Can anyone tell me what's going on here? Where's that extra 0D coming
from?


You need to open the output file in binary mode.
Your runtime system is replacing a value of 10 (which it thinks
is a carriage return character) with the sequence 13, 10
(line feed , carriage return). This translation happens
in 'text mode' and is supressed in 'binary mode'.

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 22 '05 #2
In article <ad**************************@posting.google.com >,
sh***********@hotmail.com (Colm) wrote:
It's Windows XP [...]
There's your problem. :-) Seriously, Windows thinks it's smarter than
you are and messes with your bytes.

Can anyone tell me what's going on here? Where's that extra 0D coming
from?


Open the file as "binary" and the problem should go away. In "text"
mode, the OS thinks the 10 is a linefeed delimiter and wants to turn it
into a CR/LF pair.

--
Phillip Mills
Multi-platform software development
(416) 224-0714
Jul 22 '05 #3
sh***********@hotmail.com (Colm) wrote in message news:<ad**************************@posting.google. com>...
Hi

I'm having a wierd problem and I was wondering if anyone else had
encountered something similar...

I have a program which reads in a 5 digit number from a file in
decimal format, converts it to an unsigned int and writes it back out
to another file... It works fine in most cases, but for some reason
for the number 10 it doesn't.


You've opened the file in translated (text) mode. 0x0A is the end-line
character, and in text mode on Windows, this is translated to a
carriage return/line feed pair.

Specify ios::binary when you open the file.

--
Later,
Jerry.

The universe is a figment of its own imagination.
Jul 22 '05 #4
>
0A 00
...there is actually 3 bytes...
0D 0A 00

Yet the i_bytes_written variable still claims to have written only 2
bytes.

Can anyone tell me what's going on here? Where's that extra 0D coming
from?


You need to open the output file in binary mode.
Your runtime system is replacing a value of 10 (which it thinks
is a carriage return character) with the sequence 13, 10
(line feed , carriage return). This translation happens
in 'text mode' and is supressed in 'binary mode'.


You got that backwards. :-) Character #10 is linefeed, #13 is carriage
return.

-Howard

Jul 22 '05 #5
Howard wrote:
0A 00
...there is actually 3 bytes...
0D 0A 00

Yet the i_bytes_written variable still claims to have written only 2
bytes.

Can anyone tell me what's going on here? Where's that extra 0D coming
from?


You need to open the output file in binary mode.
Your runtime system is replacing a value of 10 (which it thinks
is a carriage return character) with the sequence 13, 10
(line feed , carriage return). This translation happens
in 'text mode' and is supressed in 'binary mode'.


You got that backwards. :-) Character #10 is linefeed, #13 is carriage
return.


Oops.
It's been a long time since ....

Thanks for the correction.

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 22 '05 #6
sh***********@hotmail.com (Colm) wrote in message news:<ad**************************@posting.google. com>...
[snip]
converts it to 10 (I've checked this in debug), but when writes it,
where I was expecting to see...
0A 00
...there is actually 3 bytes...
0D 0A 00


This is not a C++ issue, but a Windows issue. You need to turn
off the thing where a bare linefeed is automatically supplied
with a carriage return.
Socks
Jul 22 '05 #7
Phillip Mills <ph************@acmDELETE.org> wrote in message news:<41**********@127.0.0.1>...
In article <ad**************************@posting.google.com >,
sh***********@hotmail.com (Colm) wrote:
It's Windows XP [...]


There's your problem. :-) Seriously, Windows thinks it's smarter than
you are and messes with your bytes.


That's simply not true -- Windows does nothing of the sort.

The translation is done by the C runtime library, and it happens
because you've _asked_ it to, not because it "thinks it's smarter than
you are". I'd also note that Windows is hardly alone in defining text
mode such that translated mode really involves doing a translation.

--
Later,
Jerry.

The universe is a figment of its own imagination.
Jul 22 '05 #8
sh***********@hotmail.com (Colm) wrote:

I'm having a wierd problem and I was wondering if anyone else had
encountered something similar...

int i_bytes_read=read(i_in_file_id, cp_decimal_num, 5);
int i_bytes_written=write(i_out_file_id, &ui_output_num, 2);

This works for any number except... for some reason if I try it with
the number 10, it doesn't work as expected. It reads in "00010",
converts it to 10 (I've checked this in debug), but when writes it,
where I was expecting to see...
0A 00
...there is actually 3 bytes...
0D 0A 00


read() and write() are non-standard functions; you should use
fopen() to open the file in binary mode, and then fread() and
fwrite() instead. If you do this then your problem will go away.
Jul 22 '05 #9
In article <b2*************************@posting.google.com> ,
jc*****@taeus.com (Jerry Coffin) wrote:
Phillip Mills <ph************@acmDELETE.org> wrote in message
news:<41**********@127.0.0.1>...
In article <ad**************************@posting.google.com >,
sh***********@hotmail.com (Colm) wrote:
It's Windows XP [...]
There's your problem. :-) Seriously, Windows thinks it's smarter than
you are and messes with your bytes.


That's simply not true -- Windows does nothing of the sort.


Actually it is *simply* true -- in other words, true for practical
purposes. It only becomes questionable when you complicate it with
unnecessary pedantry.
The translation is done by the C runtime library,
You're just specifying which part of the Windows system is being
annoying, not actually contradicting anything.
and it happens
because you've _asked_ it to,
No, it happens without being "asked" anything of the sort. It
__defaults__ to writing more bytes than it was told to and more bytes
than it does on other systems under the same conditions. If the OP had
been required to ask for that behavior, he would probably not have
joined the legions who have stumbled on it over the years.
not because it "thinks it's smarter than
you are". I'd also note that Windows is hardly alone in defining text
mode such that translated mode really involves doing a translation.


Right.... DOS, too.

--
Phillip Mills
Multi-platform software development
(416) 224-0714
Jul 22 '05 #10
Phillip Mills <ph************@acmDELETE.org> wrote in message news:<41********@127.0.0.1>...

[ ... ]
That's simply not true -- Windows does nothing of the sort.


Actually it is *simply* true -- in other words, true for practical
purposes. It only becomes questionable when you complicate it with
unnecessary pedantry.


No -- not true for practical, or any other, purposes. You might be
able to argue that it was true for practical purposes if it was
required that every possible implementation of C++ for Windows acted
the same way -- but that's not the case, and in fact there are C++
compilers available for Windows that do NOT do such a translation. As
such, it's an important distinction because if the OP used a different
compiler on Windows, he'd find different behavior. If he believe that
it was Windows itself that caused the behavior in the first place,
this would lead to still greater confusion.
The translation is done by the C runtime library,


You're just specifying which part of the Windows system is being
annoying, not actually contradicting anything.


Perhaps that's the case on your planet, but it certainly isn't true
here on planet earth.
and it happens
because you've _asked_ it to,


No, it happens without being "asked" anything of the sort. It
__defaults__ to writing more bytes than it was told to and more bytes
than it does on other systems under the same conditions. If the OP had
been required to ask for that behavior, he would probably not have
joined the legions who have stumbled on it over the years.


Quite the contrary -- the C standard specifies that translated mode is
the default, yes. That means that when you don't specify ios::binary,
you're _asking_ it to do whatever translation is needed to conform to
the local "customs" for a text file.

The fact that you've asked for this behavior tacitly doesn't change
the fact that you have asked for it. If the OP intends to learn to
program in C++, he needs to learn what parameters mean what when
calling standard library functions. Trying to teach him nonsense and
then claiming that it's true for practical purposes is NOT doing him
any favor.
not because it "thinks it's smarter than
you are". I'd also note that Windows is hardly alone in defining text
mode such that translated mode really involves doing a translation.


Right.... DOS, too.


This makes you sound narrow-minded and ignorant. In addition to the
DOS, OS/2, Windows family, MacOS, VMS, z/OS, etc. require translation
of text files. On MacOS the translation is different from on Windows,
but about equally trivial. On VMS, z/OS, and a whole host of older IBM
mainframe OSes, the translations involved are decidedly less trivial,
to put it mildly.

--
Later,
Jerry.

The universe is a figment of its own imagination.
Jul 22 '05 #11
In message <c7**************************@posting.google.com >,
pu*********@hotmail.com writes
sh***********@hotmail.com (Colm) wrote in message
news:<ad**************************@posting.google .com>...
[snip]
converts it to 10 (I've checked this in debug), but when writes it,
where I was expecting to see...
0A 00
...there is actually 3 bytes...
0D 0A 00


This is not a C++ issue, but a Windows issue. You need to turn
off the thing where a bare linefeed is automatically supplied
with a carriage return.


That *is* a C++ issue, performed by the C++ runtime library before the
data gets anywhere near Windows, and turned off by passing
std::ios_base::binary as part of the second argument to
basic_fstream::open(). See other postings in this thread.

--
Richard Herring
Jul 22 '05 #12
Thanks for your help lads I'll try that...
Jul 22 '05 #13

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

Similar topics

48
by: Joseph | last post by:
Hi I'm writing a commercial program which must be reliable. It has to do some basic reading and writing to and from files on the hard disk, and also to a floppy. I have foreseen a potential...
6
by: Sebastian Kemi | last post by:
How should a write a class to a file? Would this example work: object *myobject = 0; tfile.write(reinterpret_cast<char *>(myobject), sizeof(*object)); / sebek
3
by: ishekar | last post by:
Hi, I have an application where i want to write data to a file, the data is being sent from an external source. I know the total size of the data and then i retrieve the data in small segments...
1
by: Daniel | last post by:
System.IO.StreamWriter Close or Flush method to shut down the computer in such a way that just part of the file is written? or an empty file is written? Also if the Close or Flush is to a...
5
by: Jeong-Gun Lee | last post by:
I'm writing a code of writing a value to a specific memory address. ================================================================= #include <stdio.h> int main() { long air; long...
102
by: Xah Lee | last post by:
i had the pleasure to read the PHP's manual today. http://www.php.net/manual/en/ although Pretty Home Page is another criminal hack of the unix lineage, but if we are here to judge the quality...
16
by: Claudio Grondi | last post by:
I have a 250 Gbyte file (occupies the whole hard drive space) and want to change only eight bytes in this file at a given offset of appr. 200 Gbyte (all other data in that file should remain...
6
by: arne.muller | last post by:
Hello, I've come across some problems reading strucutres from binary files. Basically I've some strutures typedef struct { int i; double x; int n; double *mz;
3
by: Barry Flynn | last post by:
Hi I am working with a VB 2005 program which has been converted from VB6. It writes data out to a flat file, with code like the following line WriteLine(riFileNo, "Hist", lsAssetID,...
89
by: Skybuck Flying | last post by:
Hello, This morning I had an idea how to write Scalable Software in general. Unfortunately with Delphi 2007 it can't be done because it does not support operating overloading for classes, or...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
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...
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: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
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.