【问题标题】:How to append data to an existing file如何将数据附加到现有文件
【发布时间】:2020-05-02 07:55:00
【问题描述】:

在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();

但似乎没有对应于“附加”模式的选项(在IO 页面中)。目前是否没有提供,如果有,是否有任何惯用的方式来打开文件和附加数据?

【问题讨论】:

    标签: file append chapel


    【解决方案1】:

    从 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

    【讨论】:

    • 谢谢!该代码在我的计算机上运行良好(假设 foo.dat 已经存在)。
    猜你喜欢
    • 2018-11-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-24
    • 1970-01-01
    相关资源
    最近更新 更多