Haskell HgetContents未读取使用外部程序编写的数据

发布于 2025-02-09 17:50:30 字数 655 浏览 0 评论 0原文

我正在使用OpenFile打开文件句柄,使用bytestring.hputstrln将数据写入其,然后在外部编辑器中打开文件。我使用hseek寻找将句柄回到文件的开头,并希望在外部编辑器所做的更改中读取。但是,我没有看到外部编辑器中所做的更改。示例:

module Main where

import System.Process
import System.IO
import qualified Data.ByteString.Char8 as BS

main :: IO ()
main = do
  handle <- openFile "myfile" ReadWriteMode
  BS.hPutStrLn handle . BS.pack $ "hello"
  hFlush handle
  callProcess "vi" ["myfile"]
  hSeek handle AbsoluteSeek 0
  str' <- BS.hGetContents handle
  BS.putStr str'

无论我在VI时所做的变化如何,所有曾经打印到我的终端的所有内容都是“ Hello”。但是,如果然后检查文件,则会在那里进行更改。为什么hgetContents看到更改?

I am opening a file handle with openFile, writing data to it using ByteString.hPutStrLn, and then opening the file in an external editor. I seek the handle back to the beginning of the file with hSeek and expect to read back in the changes made with the external editor. However I am not seeing the changes made in the external editor. Example:

module Main where

import System.Process
import System.IO
import qualified Data.ByteString.Char8 as BS

main :: IO ()
main = do
  handle <- openFile "myfile" ReadWriteMode
  BS.hPutStrLn handle . BS.pack $ "hello"
  hFlush handle
  callProcess "vi" ["myfile"]
  hSeek handle AbsoluteSeek 0
  str' <- BS.hGetContents handle
  BS.putStr str'

All that ever gets printed to my terminal is "hello" no matter what changes I make while in vi. However, if I then examine the file, the changes are there. Why isn't hGetContents seeing the changes?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

删除→记忆 2025-02-16 17:50:30

编辑器保存时正在创建一个新文件,Haskell中的句柄正在从旧文件中读取。我的编辑器要去VIM,该VIM默认设置了writebackup功能。

关闭writebackup以及我期望的行为,以及ed

The editor is creating a new file when it saves, and the handle in Haskell is reading from the old file. My editor was going to Vim which has the writebackup feature set by default.

Turning off writebackup results in the behavior I expected, as did using ed.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文