如何将数据附加到现有文件
How to append data to an existing 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()
作为
创建频道
var cout = openwriter( "foo.dat" );
cout.writef( "n = %10i, x = %15.7r\n", 100, 1.23 );
cout.close();
但是好像没有对应"append"模式的选项(在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。
在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()
作为
var cout = openwriter( "foo.dat" );
cout.writef( "n = %10i, x = %15.7r\n", 100, 1.23 );
cout.close();
但是好像没有对应"append"模式的选项(在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。