haskell - 为什么 Haskell NoBuffering 选项似乎仍然可以缓冲?

标签 haskell ghci

我在 ghci 中加载了一个文件,内容如下:

h <- openFile "somefile.txt" ReadMode
hSetBuffering h NoBuffering

然后我在文本编辑器中修改并保存了 somefile.txt。当我在 ghci 中多次调用 hGetChar 时,我收到了文件的旧字符(好像整个文件在我打开时都被缓冲了一样)。我希望调用 hGetChar 返回修改后的内容。为什么不是这样?

编辑: 上述案例中修改后的内容没有显示出来,确实是文本编辑器的问题。当改用 cat 命令时 (cat > somefile.txt),然后返回修改后的文件内容。

但是,它似乎仍然在做缓冲。假设文件内容如下:

ABCDEFGHI
123456789

如果我运行 hGetChar,我会得到预期的“A”。

现在,如果我使用 cat (cat > somefile.txt) 将内容更改为以下内容,然后再次运行 hGetChar,我会期待“Z”,但它返回“B”:

AZZZZZZZZ

最佳答案

BufferMode 仅在写入句柄时相关,而不是从句柄读取时。

来自 GHC.IO.Handle.Types 中的 [note Buffered Reading] :

Note that the buffering mode (haBufferMode) makes no difference when
reading data into a Handle.  When reading, we can always just read all
the data there is available without blocking, decode it into the Char
buffer, and then provide it immediately to the caller.

输入文档BufferMode似乎已经过时了。

关于haskell - 为什么 Haskell NoBuffering 选项似乎仍然可以缓冲?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37644359/

相关文章:

haskell - 使用 Haskell 修改数字类型

haskell - ghci 中的 pretty-print

haskell - GHCi 调试器没有递归地命中断点 : Why, 解决方案是什么?

haskell - 无法在我的 Ubuntu Linux 机器上安装 hmatrix

algorithm - haskell中的递归函数

haskell - 如何在 QuickCheck (Haskell) 中使用 'oneof'

haskell - 在 GHCi 中定义函数签名

haskell - :sprint and `seq` together - missing evaluation?

Haskell 编译器错误 : not in scope

haskell - 基于范数约束向量的类型