append a text on the top of the file

前端 未结 6 880
借酒劲吻你
借酒劲吻你 2020-12-20 16:29

I want to add a text on the top of my data.txt file, this code add the text at the end of the file. how I can modify this code to write the text on the top of my data.txt f

6条回答
  •  不思量自难忘°
    2020-12-20 16:53

    Appending to the top is normally called prepending.

    open(M,"<","data.txt");
    @m = ;
    close(M);
    open(M,">","data.txt");
    print M "foo\n";
    print M @m;
    close(M);
    

    Alternately open data.txt- for writing and then move data.txt- to data.txt after the close, which has the benefit of being atomic so interruptions cannot leave the data.txt file truncated.

提交回复
热议问题