File Operations:
Below are the few file operations which we perform generally
1) Crete,Read, Write, Open and Close
Note: Other than .pdf we can create .txt, csc, .doc , .xls, .dat
Create a file and write data into that file:
Action();
{
char *file1 = D:\\madhu\\sample.txt;
long filename1;
filename1=fopen(file1,"w")--------->W is used to write data into file and R is used to Read.
fprintf(filename1,"%s","Welcome To Load Runner");-->fprintf is used to enter some text into a file
fclose(filename1);--->flose is used to close the opened fie
return 0;
}
Write Some Random Numbers into a file:
Action();
{
char *file1 = D:\\madhu\\sample.txt;
long filename1;
int a=rand()%11+10;
filename1=fopen(file1,"w")--------->W is used to write data into file and R is used to Read.
fprintf(filename1,"%d\n",a);
fclose(filename1);
return 0;
}
Read data from an existing file:
Read data in a file as string wise using common separator
Action();
{
char *file1 = D:\\madhu\\sample.txt;
long filename1;
char s1[100],s2[100],s3[100],s4[100];
int d;
filename1=fopen(file1,"w")--------->W is used to write data into file and R is used to Read.
fprintf(filename1,"%s","Welcome To Load Runner performance testing version 1")
filename1=fopen(file1,"r")--------->W is used to write data into file and R is used to Read.
fscanf(filename1,"%s1",&s1);
fscanf(filename1,"%s2",&s2);
fscanf(filename1,"%s3",&s3);
fscanf(filename1,"%s4",&s4);
fscanf(filename1,"%d",&d);
lr_output_message("%s %s %s %s ",&s1,&s2,&s3,&s4);
lr_output_message("%d ",&d);
fclose(filename1);
return 0;
}
Below are the few file operations which we perform generally
1) Crete,Read, Write, Open and Close
Note: Other than .pdf we can create .txt, csc, .doc , .xls, .dat
Create a file and write data into that file:
Action();
{
char *file1 = D:\\madhu\\sample.txt;
long filename1;
filename1=fopen(file1,"w")--------->W is used to write data into file and R is used to Read.
fprintf(filename1,"%s","Welcome To Load Runner");-->fprintf is used to enter some text into a file
fclose(filename1);--->flose is used to close the opened fie
return 0;
}
Write Some Random Numbers into a file:
Action();
{
char *file1 = D:\\madhu\\sample.txt;
long filename1;
int a=rand()%11+10;
filename1=fopen(file1,"w")--------->W is used to write data into file and R is used to Read.
fprintf(filename1,"%d\n",a);
fclose(filename1);
return 0;
}
Read data from an existing file:
Read data in a file as string wise using common separator
Action();
{
char *file1 = D:\\madhu\\sample.txt;
long filename1;
char s1[100],s2[100],s3[100],s4[100];
int d;
filename1=fopen(file1,"w")--------->W is used to write data into file and R is used to Read.
fprintf(filename1,"%s","Welcome To Load Runner performance testing version 1")
filename1=fopen(file1,"r")--------->W is used to write data into file and R is used to Read.
fscanf(filename1,"%s1",&s1);
fscanf(filename1,"%s2",&s2);
fscanf(filename1,"%s3",&s3);
fscanf(filename1,"%s4",&s4);
fscanf(filename1,"%d",&d);
lr_output_message("%s %s %s %s ",&s1,&s2,&s3,&s4);
lr_output_message("%d ",&d);
fclose(filename1);
return 0;
}
Read data in a file as line wise :
Action();
{
char *file1 = D:\\madhu\\sample.txt;
long filename1;
char temp[100];
filename1=fopen(file1,"w")--------->W is used to write data into file and R is used to Read.
fprintf(filename1,"%s","Welcome To Load Runner performance testing version 1");
filename1=fopen(file1,"r")--------->W is used to write data into file and R is used to Read.
fgets(temp,200,filename1);
fclose(filename1);--->flose is used to close the opened fie
return 0;
}
To Read size of characters in file
Action();
{
char *file1 = D:\\madhu\\sample.txt;
long filename1;
char temp[100];
int total=0;count;
char buffer[1000];
filename1=fopen(file1,"w")--------->W is used to write data into file and R is used to Read.
fprintf(filename1,"%s","Welcome To Load Runner performance testing version 1");
filename1=fopen(file1,"r")--------->W is used to write data into file and R is used to Read.
while(!feof(filename1))
{
count=fread(buffer,sizeof(char),1000,filename1);
total +=count;
}
lr_output_message("total number of bytes=%d ",total);
fclose(filename1);--->flose is used to close the opened fie
return 0;
}
{
char *file1 = D:\\madhu\\sample.txt;
long filename1;
char temp[100];
filename1=fopen(file1,"w")--------->W is used to write data into file and R is used to Read.
fprintf(filename1,"%s","Welcome To Load Runner performance testing version 1");
filename1=fopen(file1,"r")--------->W is used to write data into file and R is used to Read.
fgets(temp,200,filename1);
fclose(filename1);--->flose is used to close the opened fie
return 0;
}
To Read size of characters in file
Action();
{
char *file1 = D:\\madhu\\sample.txt;
long filename1;
char temp[100];
int total=0;count;
char buffer[1000];
filename1=fopen(file1,"w")--------->W is used to write data into file and R is used to Read.
fprintf(filename1,"%s","Welcome To Load Runner performance testing version 1");
filename1=fopen(file1,"r")--------->W is used to write data into file and R is used to Read.
while(!feof(filename1))
{
count=fread(buffer,sizeof(char),1000,filename1);
total +=count;
}
lr_output_message("total number of bytes=%d ",total);
fclose(filename1);--->flose is used to close the opened fie
return 0;
}
Total no.of characters of a file :
Action();
{
char *file1 = D:\\madhu\\sample.txt;
long filename1;
char temp[100];
filename1=fopen(file1,"w")--------->W is used to write data into file and R is used to Read.
fprintf(filename1,"%s","Welcome To Load Runner performance testing version 1");
fprintf(filename1,"%s","Welcome To Load Runner performance testing version 2");
fprintf(filename1,"%s","Welcome To Load Runner performance testing version 3");
filename1=fopen(file1,"r")--------->W is used to write data into file and R is used to Read.
while(!feof(filename1))
{
lr_output_message("%s ",temp);
fgets(temp,1000,filename1);
}
fclose(filename1);--->flose is used to close the opened fie
return 0;
}
{
char *file1 = D:\\madhu\\sample.txt;
long filename1;
char temp[100];
filename1=fopen(file1,"w")--------->W is used to write data into file and R is used to Read.
fprintf(filename1,"%s","Welcome To Load Runner performance testing version 1");
fprintf(filename1,"%s","Welcome To Load Runner performance testing version 2");
fprintf(filename1,"%s","Welcome To Load Runner performance testing version 3");
filename1=fopen(file1,"r")--------->W is used to write data into file and R is used to Read.
while(!feof(filename1))
{
lr_output_message("%s ",temp);
fgets(temp,1000,filename1);
}
fclose(filename1);--->flose is used to close the opened fie
return 0;
}