恐惧行为 [英] fread behaviour

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

问题描述

在以文本模式"打开的文件上使用fread时应如何表现.是将\ r \ n对压缩成单个\ n还是要逐字读取文件.

此外,如果我以文本模式打开文件进行写入. fwrite会始终将\ n扩展为\ r \ n.

脚注:
\ n,\ r \ n问题仍然贯穿整个软件行业.在事后看来,我认为Microsoft寻求\ r \ n是灾难性的决定.这不是他们的文化,他们在为Commodore 64编写Basic时没有这样做.顺便说一句,当时他们也有一个美元终止字符串的概念.也许有人当时觉得,真正的程序员使用\ r \ n而不是\ n.我们今天要承受后果!

:-D

解决方案

您说:

\ n,\ r \ n问题仍然贯穿整个软件行业.在事后看来,我认为Microsoft寻求\ r \ n是灾难性的决定.


但是,事实并非如此:这不是Microsoft的决定!请查看以下链接:

换行符-维基百科,免费的百科全书 [ Windows,UNIX和Macintosh文本文件中的换行符 [
干杯,

Ash


我的理解是 fread 不在乎文件模式-文本还是二进制.如果您在此处 [ 解决方案

You said:

The \n, \r\n issue still permeates through the software industry. It was I think in hindsight a disasterous decission by Microsoft to go for \r\n.


But this is not so true: it is not a Microsoft decision! Please, have a look to the links below:

Newline - Wikipedia, the free encyclopedia[^]
Line Breaks in Windows, UNIX and Macintosh Text Files[^]

The \r\n representation for newline was already in use before that Microsoft was founded! And not only: Macintosh systems prior to MacOS X used \r instead than \r\n and \n!


When you read a file in text mode the C90 standard (as implemented by Microsoft''s compilers) says the implementation can do what it likes when it reads and write the file.

This means all bets are off if you try and use fread with a file opened in text mode. Want to read that unixy file with 0xDA sequences in on Windows? Use a binary file.

As Sauro mentioned Macs on OS9 and earlier translated ''\r'' to ''\n'' when they read and people managed to write portable code. The general rule is if you open a file in text mode use things like fprintf and fscanf for formatted input and output. If you open it in binary mode use fread and fwrite as the raw bytes won''t be scrambled.

Cheers,

Ash


It''s my understanding that fread doesn''t care about file mode - text vs binary. If you look here[^] you''ll see that fread''s behaviour is defined in terms of multiple calls to fgetc, which in turn makes no mention of end of line mappings, just a stream of bytes (with a comment that bytes and characters aren''t necessarily the same).

I think a similar thing applies to fwrite.

Footnote:
I''m old enough to remember when teletypes screwed up if you didn''t use \r\n, or even \r\n\0xff\0xff\0xff on some machines. (The carriage returned slowly.) To me, Unix/C using just \n was the aberration, and things haven''t improved in the subsequent 30-some years, as you observe.


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

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