首页 > 解决方案 > 如何将数据附加到现有文件

问题描述

在 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页面中)。目前是否没有提供,如果有,是否有任何惯用的方法来打开文件和附加数据?

标签: fileappendchapel

解决方案


从 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


推荐阅读