脚本开发-从文件读取数据并参数化
直接上代码:
char* testfn()
{
int count, total = 0;
//char buffer[1000];
char * buffer = NULL;
int filelenth = 0;
long file_stream;
char * filename = "d:\\test.txt";
if ((file_stream = fopen(filename, "r")) == NULL )
{
lr_error_message ("Cannot open %s", filename);
return NULL;
}
fseek(file_stream,0,2); //定位到文件末尾
filelenth = ftell(file_stream); //获取文件总长度
fseek(file_stream,0,0); //定位文件开头
buffer = (char *)malloc(filelenth); //动态分配内存
count = fread(buffer, sizeof(char), filelenth, file_stream); //
lr_output_message ("%d bytes read", count);
lr_output_message ("content read = %s", buffer );
// Close the file stream
if (fclose(file_stream))
{
lr_error_message ("Error closing file %s", filename);
}
return buffer;
}
Action()
{
char *pt = NULL;
pt = testfn();
lr_save_string(lr_eval_string(pt), "param");
lr_output_message("value of param: %s",lr_eval_string("{param}"));
free(pt); //释放内存
}
运行结果: