写作10 [英] Writing 10

查看:71
本文介绍了写作10的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我遇到了一个奇怪的问题,我想知道是否有其他人有b $ b遇到过类似的东西......


我有一个程序从一个文件中读取一个5位数的数字,用
十进制格式,将它转换为unsigned int并将其写回

到另一个文件......在大多数情况下它工作正常,但由于某种原因

为数字10它不会。


它是使用Borland C ++的Windows XP,代码类似......


char cp_decimal_num [5];

int i_bytes_read = read(i_in_file_id ,cp_decimal_num,5);

if(i_bytes_read!= 5)

perror(阅读错误);


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(写错误);


现在大多数情况下它读入五数字编号,(例如00100,

将其转换为unsigned int(100),然后将2个字节写入

输出文件(64,00)。


这适用于任何数字,除了......出于某种原因,如果我尝试使用

数字10,它没有按预期工作。它读入00010,

将其转换为10(我已经在调试中检查了这一点),但是在写入时,

我希望看到的地方...

0A 00

....实际上有3个字节...

0D 0A 00


然而i_bytes_written变量仍然声称只写了2个

字节。


谁能告诉我这里发生了什么?那个额外的0D来自

来自?


谢谢

Colm

解决方案

Colm写道:



char cp_decimal_num [5];


char cp_decimal_num [6];


***


你需要有空间终止''\ 0''字符,atoi

预计会在那里。

cp_decimal_num [5] =''\ 0'';

int i_bytes_read = read(i_in_file_id,cp_decimal_num,5);
if(i_bytes_read!= 5)
perror(错误读取);

unsigned int ui_output_num = atoi(cp_decimal_num);

[snip]

0A 00
...实际上有3个字节...
0D 0A 00

然而,i_bytes_written变量仍声称只写了2个字节。

谁能告诉我这里发生了什么?那个额外的0D来自哪里?




你需要以二进制模式打开输出文件。

你的运行时系统正在替换值10(它认为

是一个回车符),序列为13,10 * / b $ b $(换行,回车)。这种翻译在''文本模式'中发生了
,并在''二进制模式'中被抑制。


-

Karl Heinz Buchegger
kb******@gascad.at


在文章< ad ************************** @ posting.google.com>中,
sh***********@hotmail.com (科尔姆)写道:

它是Windows XP [...]


这是你的问题。 :-)说真的,Windows认为它比你更聪明,而且你的字节很混乱。


谁能告诉我发生了什么事在这?那个额外的0D来自哪里?




将文件打开为二进制文件问题应该消失。在text

模式下,操作系统认为10是一个换行分隔符,并希望将它变成一个CR / LF对。


-

Phillip Mills

多平台软件开发

(416)224-0714


sh *********** @ hotmail。 com (Colm)在留言新闻中写道:< ad ************************** @ posting.google。 com> ...



我有一个奇怪的问题,我想知道是否还有其他人遇到类似的东西......

我有一个程序,它从一个十进制格式的文件中读取一个5位数的数字,将它转换为unsigned int并将其写回另一个文件......它在大多数情况下工作正常,但由于某种原因,数字10不会。




你已经翻译了文件(文字)模式。 0x0A是结束行

字符,在Windows上的文本模式下,这被转换为

回车/换行符对。

当你打开文件时指定ios :: binary。


-

后来,

杰瑞。


宇宙是自己想象的虚构。


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

解决方案

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


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


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.


这篇关于写作10的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆