LR File Manipulation Functions examples

上一篇 / 下一篇  2012-06-04 16:01:24 / 个人分类:LoadRunner

File Manipulation functions allow you to manipulate directories. Expand the category to view a list of the available functions.

Click one of the following functions for more information:

Function Name

Description

chdir

Changes the current directory to the given path.

chdrive

Switches to another drive.

getcwd

Returns the name of the current working directory.

getdrive

Returns the name of the current drive.

mkdir

Creates a directory using the given path name.

remove

Deletes the specified file.

rmdir

Deletes the specified directory.

 

 

 

 

例子:The following example uses mkdir to create a new directory,xyz. It then moves to the new directory.

#ifdef unix

char new_dir[]="/tmp/xyz"

#else

char new_dir[]="C:\\xyz";

#endif

 

//加载外部函数

lr_load_dll("D:\Accessdll.dll");

//动态文件中包含如下函数

/*int file_exists(char *filename)

return (access(filename,0)==0);*/

 

//判断目录是否存在可以用

if(file_exists(new_dir)) {

lr_output_message("%s directory exists",new_dir);

rmdir(new_dir);

}

//root目录下创建xyz目录,并设置其为当前目录

if (mkdir(new_dir)) {

        lr_output_message("Create directory %s failed",new_dir);

        return -1;

}

 

else

lr_output_message("Created new directory %s",new_dir);

 

//变更目录

if (chdir(new_dir)) {

        lr_output_message("Unable to change to dir %s",new_dir);

        return -1;

}

else

lr_output_message("Changed to new dir %s",new_dir);*/

 

 

 

例子:The following example uses chdrive to move to all available drives on the file system.

It saves the original drive in curdrive, so it can be restored later.

int drive,curdrive;

//获取当前的盘符

curdrive=getdrive();

lr_output_message("Available drives are:");

//循环读取本系统中所有的盘符

for(drive=1;drive<=26;drive++)

        if(!chdrive(drive))

          lr_output_message("%c:",drive + 'A'-1);

        chdrive(curdrive);

 

 

 

例子:This example uses getcwd to save the current directory, before moving to a new directory.

It then moves back to the first directory.

#define DIR_LEN 512

char original_dir[DIR_LEN];

#ifdef unix

char new_dir[]="/tmp";

#else

char new_dir[]="C:\\";

#endif

//getcwd如果成功返回当前的字符串,否则返回空值

if (!getcwd(original_dir,DIR_LEN)) {

lr_output_message("getcwd error");

return -1;

}

else

lr_output_message("The current dir is: %s",original_dir);

if (chdir(new_dir)) {

        lr_output_message("Unable to locate directory:%s",new_dir);

        return -1;

}

else

lr_output_message("Changed dir to %s",new_dir);

if (chdir(original_dir)) {

        lr_output_message("Cannot move back to #s",original_dir);

        return -1;

}

else

lr_output_message("Move back to %s",original_dir);

 

 

 

例子:/*The system function executes a dir command.

The output of the command is written (using `>) to the newly created file.

The content of the file should be a list of the contents of the C drive.

It then deletes the new file with remove*/

 

char filename[1024],command[1024];

char new_dir[]="C:\\xyz";

extern int errno;

 

//root下创建xyz目录,并设为当前目录

if(mkdir(new_dir))

        lr_output_message("Create directory %s failed",new_dir);

else

lr_output_message("Created new directory %s",new_dir);

//filename赋值为C:\xyz\newfile.txt

sprintf(filename,"%s\\%s",new_dir,"newfile.txt");

//command赋值为dir C:\ > C:\xyz\newfile.txt /w

sprintf(command,"dir /b C:\\ > %s /w",filename);

//运行命令:dir /b C:\ > C:\xyz\newfile.txt /w

system(command);

lr_output_message("Create new file %s",filename);

 

//删除已经建立的文件

if (remove(filename) == 0)

       lr_output_message ("Removed new file %s", filename);

   else

       lr_output_message ("Unable to remove %s error %d", filename, errno);

 

例子:/*在windows系统,用LR中的system函数执行系统命令,在LR中利用C函数建立文件,写文件,读文件。
  主要相关函数:sprintf/fopen/fgetc/fread/fclose*/
  int count,total=0,i;
     char buffer[1000];
     long file_stream;
     char filename[1024], command[1024],line[100];
     char new_dir[] = "C:\\test";
     if (mkdir(new_dir))
          lr_output_message("Create directory %s failed", new_dir);
     else
          lr_output_message("Created new directory %s", new_dir);
     sprintf (filename, "%s\\%s", new_dir, "newfile.txt");
     sprintf (command, "dir /b c:\\ > %s /w", filename );
     system(command);//通过DOS管道命令创建TXT文件
     lr_output_message("Created new file %s", filename);
  //以只读方式打开文件
   if((file_stream=fopen(filename,"r"))==NULL)
       {
       lr_error_message("can not open %s",filename);
       return -1;
   }
   //逐行读取TXT文件中的字符串
   for(i=1;i<10;i++)
    {
       if (fgets(line, 100, file_stream) == NULL)
          lr_output_message("fgets error" );
     else 
          lr_output_message( "The line%d is: %s",i,line);
    }
//如果已经到文件结尾,计算总得字节数
    while(!feof(file_stream))
              {
      count=fread(buffer,sizeof(char),1000,file_stream);
       lr_output_message("%3d read",count);

 //Checks if any error has occurred during file I/0.
    if(ferror(file_stream))
              {
    lr_output_message("error reading file %s",filename);
    break;
        }
//把计算后的Count赋值给total
   total+=count;
        }
      lr_output_message("Total number of bytes read = %d",total);

     if(fclose(file_stream))//关闭文件
      lr_error_message("Error closing file %s",filename);


TAG:

 

评分:0

我来说两句

Open Toolbar