file - 如何将数据附加到现有文件
问题描述
在 Chapel 中,我们可以使用open()
+打开一个文件进行写入iomode.cw
,例如,
var fout = open( "foo.dat", iomode.cw ); // create a file for writing
var cout = fout.writer(); // make a channel
cout.writeln( 1.23 );
cout.close();
fout.close();
openwriter()
或通过as制作频道
var cout = openwriter( "foo.dat" );
cout.writef( "n = %10i, x = %15.7r\n", 100, 1.23 );
cout.close();
但似乎没有对应于“附加”模式的选项(在IO页面中)。目前是否没有提供,如果有,是否有任何惯用的方法来打开文件和附加数据?
解决方案
从 Chapel 1.20 开始不支持 IO 的附加模式。在受支持之前,您可以使用以下解决方法:
// Open a file for reading and writing
var fout = open("foo.dat", iomode.rw);
// Position a writing channel at the end of the file
var cout = fout.openAppender();
cout.writeln(1.23);
cout.close();
fout.close();
/* Create a writer channel with a starting offset at the end of the file */
proc file.openAppender() {
var writer = this.writer(start=this.length());
return writer;
}
在 Chapel GitHub 问题中有一个附加模式的开放功能请求。有关详细信息,请参阅问题#9992。
推荐阅读
- c - 如何在 PostgreSQL 中使用 [libpg] "COPY FROM STDIN" 方法插入多条目数组?
- python - 在 macOS 上更新 tcl/tk 版本的 Homebrew python3
- ios - 如何在应用程序终止然后在 Xcode 中重新打开后对其进行调试?
- javascript - node.js 将方法(或函数)添加到对象 - 意外标识符
- python - 名称“未定义”,即使它是,在一个单独的函数中
- spring - 什么时候使用 org.springframework.format.Printer 接口?
- keras - Word2vec 中的自定义损失函数
- java - 为什么从服务器推送通知时未执行“MyFirebaseMessagingService”类
- python-3.x - 捕获线程输出以将其重定向到 Python 中的 GUI(wxpython)
- python - YouTube API Python upload_video.py 从脚本而不是命令行运行