在dm脚本中将文本附加到文件的最佳方式



向文件追加行的最佳方式是什么?


当前我正在使用以下脚本:

/**
* Append the `line` to the file given at the `path`.
* 
* @param path 
*     The absolute or relative path to the file with 
*     extension
* @param line
*     The line to append
* @param [max_lines=10000]
*     The maximum number of lines to allow  for a file 
*     to prevent an infinite loop
*/
void append(string path, string line, number max_lines){
number f = OpenFileForReadingAndWriting(path);
// go through file until the end is reached to set the 
// internal pointer to this position
number line_counter = 0;
string file_content = "";
string file_line;
while(ReadFileLine(f, file_line) && line_counter < max_lines){
line_counter++;
// file_content += file_line;
}

// result("file content: n" + file_content + "{EOF}");

// append the line
WriteFile(f, line + "n");
CloseFile(f);
}
void append(string path, string line){
append(path, line, 10000);
}
string path = "path/to/file.txt";
append(path, "Appended line");

对我来说,读取整个文件内容只附加一行似乎有点奇怪。如果文件很大,这可能非常慢1。所以我想有一个更好的解决方案。有人知道这个解决方案吗?


一些背景

我的应用程序是用python编写的,但在Digital Micrograph中执行。我的python应用程序正在记录其步骤。有时我从python执行dm脚本。我不可能看到发生了什么。因为有一个bug,我需要一些东西来了解发生了什么,所以我也想在dm-script中添加日志记录。

这也解释了为什么我每次都要打开和关闭文件。这需要更多的时间,但我并不关心调试时的执行速度。正常版本的日志会像往常一样被删除或关闭。但另一方面,我交替执行dm-scriptpython,因此我必须防止python阻塞dm-script的文件,反之亦然。


1正如背景中所写的,我对速度并不感兴趣。所以目前的剧本对我来说已经足够了。但我仍然对如何做得更好感兴趣,只是为了学习和好奇。

处理DM脚本中任何文件(二进制或文本(的最佳方法是使用流对象。以下示例应回答您的问题:

void writeText()
{
string path
if ( !SaveAsDialog( "Save text as" , path , path ) ) return
number fileID = CreateFileForWriting( path )
object fStream = NewStreamFromFileReference( fileID , 1 )   // 1 for auto-close file when out of scope
// Write some text
number encoding = 0 // 0 = system default
fStream.StreamWriteAsText( encoding , "The quick brown dog jumps over the lazy fox" )
// Replace last 'fox' by 'dog'
fStream.StreamSetPos( 1 , -3 )        // 3 bytes before current position
fStream.StreamWriteAsText( encoding, "dog" )

// Replace first 'dog' by 'fox'
fStream.StreamSetPos( 0 , 16 )        // 16 bytes after start
fStream.StreamWriteAsText( encoding, "fox" )
// Append at end
fStream.StreamSetPos( 2 , 0 )        // end position (0 bytes from end)
fStream.StreamWriteAsText( encoding, "." )
}
writeText()

最新更新