Haskell HgetContents未读取使用外部程序编写的数据
我正在使用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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
编辑器保存时正在创建一个新文件,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 usinged
.