1、fscanf 整行写入函数
myfile = fopen("D:\\CWMS.TXT","r") =>以只读的方式打开文件
fscanf(myfile,"%s",data) =>将文件内容传给数组
lr_output_message("%s",data);
2、fwrite 写入函数
size_t fwrite(const void* buffer, size_t size, size_t count, FILE* stream);
返回值:返回实际写入的数据块数目
(1)buffer:是一个指针,对fwrite来说,是要获取数据的地址;
(2)size:要写入内容的单字节数;
(3)count:要进行写入size字节的数据项的个数;
(4)stream:目标文件指针;
(5)返回实际写入的数据项个数count。
说明:写入到文件的哪里? 这个与文件的打开模式有关,如果是w+,则是从file pointer指向的地址开始写,替换掉之后的内容,文件的长度可以不变,stream的位置移动count个数;如果是a+,则从文件的末尾开始添加,文件长度加大。
fseek对此函数有作用,但是fwrite[1] 函数写到用户空间缓冲区,并未同步到文件中,所以修改后要将内存与文件同步可以用fflush(FILE *fp)函数同步。
#include <stdio.h>
struct
mystruct
{
int
i;
char
cha;
};
int
main(
void
)
{
FILE
*stream;
struct
mystruct s;
if
((stream =
fopen
(
"TEST.$$$"
,
"wb"
)) == NULL)
/* open file TEST.$$$ */
{
fprintf
(stderr,
"Cannot open output file.\n"
);
return
1;
}
s.i = 0;
s.cha =
'A'
;
fwrite
(&s,
sizeof
(s), 1, stream);
/* 写的struct文件*/
fclose
(stream);
/*关闭文件*/
return
0;
}
3、将文件1的内容复制到文件2 (fscanf 读,fprintf 写)
int k=0;
char data[1000];
long myfile1 ;
long myfile2;
myfile1 = fopen("D:\\CWMS1.TXT","r");
myfile2 = fopen("D:\\CWMS2.TXT","w");
if( myfile1 == null ){
lr_error_message("can't open this file");
}
do{
fscanf(myfile1,"%s",data);
lr_output_message("Line%d:%s",k,data);
fprintf(myfile2,"%s\n",data);
}while( feof(myfile1 ) != 1 ) ;
fclose(myfile1);
fclose(myfile2);
注:loadrunner不支持FILE类型,可以使用long类型代替;
如果非要使用FILE,则可以在前面增加 #include<stdio.h>之后,就可以使用 file *myfile1 。
4、通过关联获取下载文件,然后写入到本地
long lfbody ; 定义缓存文件
int iflen;
web_reg_save_param("downFile"...........);
web_rul("下载页面","http://1202.........");
//获取下载文件大小
iflen = web_get_int_property(HTTP_INFO_DOENLOAD_SIZE);
if( iflen > 0){
//以写的方式打开文件
if( ( lfbody = fopen("d:\\cwms.exe","wb") ) == NULL){
lr_output_message("文件操作失败");
return -1;
} }
//写入文件
fwrite(lr_eval_string("{downFile}"),iflen,1,lfbody );
fclose(lfbody ) ;