深入了解c/c++输入输出相关。转两篇
C++ 构造函数小错误备记

c语言文件的写入方式引起的bug

皮贝贝 posted @ 2009年11月11日 18:45 in 岁月舍利 with tags c , 2115 阅读

今天犯了一个严重的低级错误,看下面代码:

 

 

	FILE* pFile = fopen( "test.dat", "w" );
	int nValue = 10;
	fwrite( &nValue, sizeof( nValue ), 1, pFile );
	fclose( pFile );

 

我计划将整型的10 写入文件,理想状态下是文件四个字节:

 

  0A 00 00 00

 

 

结果文件内容是这样:

 

0D 0A 00 00 00

 

才发现 fopen 的方式 应该改为 "wb" , 文本方式下会将  0x0A 在windows平台上转化成 0x0D 0x0A。

 

 

 

 

 

 

Avatar_small
pavzi.com 说:
2024年1月27日 17:28

Pavzi.com provides all the news about Gadgets, the Economy, Technology, Business, Finance and many more. The main concept or our aim behind this website has been the will to provide resources with full information on each topic which can be accessed through the Internet. To ensure that every reader gets what is important and worthy about the topic they search and link to hear from us. pavzi.com Our site is a multiple Niche or category website which will ensure to provide information and resources on each and every topic. Some of the evergreen topics you will see on our website are Career, Job Recruitment, Educational, Technology, Reviews and others. We are targeting mostly so it is true that Tech, Finance, and Product Reviews. The only reason we have started this website is to make this site the need for your daily search use.


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter