代码的意外行为 [英] Unexpected behavior of Code

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

问题描述

请参阅以下程序.我从来不用处理这个.....

Please see the following program. I have never have to deal with this.....

int _tmain(int argc, _TCHAR* argv[])
{
	FILE *f,*f2;
	char c[2];
	f=fopen("list.txt","w");
	c[0]=13;
	c[1]=0;

	fwrite(&c[0],1,1,f);
	fclose(f);

	f2=fopen("list.txt","r");
	while(fread(&c[0],1,1,f2))
	{
		printf("%d",c[0]);
	}
	return 0;
}


int _tmain(int argc, _TCHAR* argv[])
{
	FILE *f,*f2;
	char c[2];
	f=fopen("list.txt","w");
	c[0]=10;
	c[1]=0;

	fwrite(&c[0],1,1,f);
	fclose(f);

	f2=fopen("list.txt","r");
	while(fread(&c[0],1,1,f2))
	{
		printf("%d",c[0]);
	}
	return 0;
}


看上面的代码

我一直对这个愚蠢的问题感到困惑

第一个代码生成大小为1个字节的文件.没问题
但是第二段代码创建了2个字节的文件
在第二部分中,打开文件并读取所有字符.它只能读取一个字符,但是如果我使用notepad ++打开文件,则会发生意外,Notepad ++会显示\ r \ n ????????????的字符序列.


look at the code above

I have been confused with this silly issue

First code generate the file with the size of 1 byte. No problem with that
but the second code create the file with 2 bytes
in the second part, where file is opened and read all the character. it only read one character, but if I open the file with notepad++ then surprise happen, Notepad++ shows a character sequence of \r\n???????????

推荐答案

该文件仅包含一个字符,即您使用fwrite()编写的一个字符.

记事本通过使用裸露的换行"(十进制字符10,十六进制0A,也称为\ n)并放入回车"字符(十进制13,十六进制0D,也称为\ r来帮助您" )使其更易读.

许多编辑器通过将其显示为多行(在技术上来说是较长的长行)来帮助您可视化该文件.记事本和写字板通常会向您显示同一文件的两种不同布局.那是由于\ r \ n约定.

要问自己的问题是除了记事本,您还有什么证据证明文件大小为2个字符"?

-----------------------------------

好吧,我花了一些时间在Windows 7上使用VS2008运行您的代码.当我在while循环内的"printf"语句上设置断点时,每个变体仅到达那里一次(使用10或13作为数据).这意味着fread语句只得到1个字符,然后得到EOF,而不是您所说的两个字符.而且打印的值完全符合预期.

我确实注意到,文件属性"窗口显示文件大小为2字节,但您发现只读字符只有1个字符.我也有点不知所措.

-----------------------------------

阅读了Richard的解决方案后,我决定检查文档.它说:
对于文本文件,根据应用程序运行的环境,在输入/输出操作中可能会进行一些特殊的字符转换,以使它们适应于系统特定的文本文件格式.
哪种类型的权限可将内容添加到您的输出中(LF-> CRLF).

当我更改您的应用程序以使用"wb"/"rb"作为模式时,它会按您期望的方式工作.
The file contains one and only one character, the one you wrote with fwrite().

Notepad is "helping" you by taking the bare-naked "new line" (decimal character 10, hex 0A, also known as \n) and putting a "carriage return" character (decimal 13, hex 0D, also known as \r) in front of it to make the line readable.

Many editors help you to visualize the file by showing it as multiple lines when, technically, it''s on big long line. Often Notepad and Wordpad will show you two different layouts of the same file. That''s due to the \r\n conventions.

The question to ask yourself is "what evidence do you have, other than notepad, that the file is sized as 2 characters"?

-----------------------------------

Well, I had some time to run your code with VS2008 on Windows 7. When I set a breakpoint on your "printf" statement inside the while loop, I get there only once in each variation (using 10 or 13 as the data). This implies that the fread statement is getting only 1 character and then EOF, not two characters as you are saying. And the value printed is exactly as expected.

I did notice that the "file properties" window shows the file has being 2 bytes as you found yet the read only got one character. I''m at a bit of a loss on that one too.

-----------------------------------

Having read Richard''s solution, I decided to check the documentation. It says :
In the case of text files, depending on the environment where the application runs, some special character conversion may occur in input/output operations to adapt them to a system-specific text file format.
which kind of gives it permission to add stuff to your output (LF -> CRLF).

When I change your application to use "wb" / "rb" as the modes, it works as you expect.


不要使用外部编辑器(记事本等)查看数据文件不是纯文本行.给自己一个十六进制查看器/编辑器,它不会尝试为您解释内容,而只是将其显示为一系列字节.
Don''t use external editors (Notepad etc) to view data files that are not pure text lines. Get yourself a hex viewer/editor that does not try to interpret the content for you, but merely displays it as a series of bytes.


请按以下方式更改第二个程序,
第六行:
c [0] = 13;

然后再次测试输出文件.
因为某些程序无法将0x0A识别为新行,因此它们只能看到0x0D.
please change the second program as this ,
6th line :
c[0]=13;

and test the output file again.
because some programs doesn''t recognize 0x0A as a new line and they should only see 0x0D.


这篇关于代码的意外行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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