Wednesday, 14 December 2022

Load runner program to copy file from one folder to another

char * filename ="D:\\File.txt";  // add \\ quote instead of single (\) for defining path.

char * filename2="D:\\TestFolder\\TestFile.txt";    // single (\) needed to terminate buy other single quote ( i.e. \\)
long file, FileVarriable;
char names[200];

Action()
{    
    fopen(filename, "r");    //open first filename with read only mode
     FileVarriable = fopen (filename2,"w+");  //open second filename with read and write permission mode
    
    if ((file = fopen(filename, "r")) == NULL) {
    lr_error_message ("Cannot open File = %s or File Does not exist", file);
    return -1; }
    
    while (!feof(file))
    {
        fgets(names, 200, file);  // here 200 decide the length of the contents in a line. So if you have bigger length to read, then you need to increase the length.
        
        if (!feof(file)) //if not at the end of file print names
        {
//            lr_output_message("%s",lr_eval_string(names));
            fprintf (FileVarriable, "%s"lr_eval_string(names));
        }
    }
    fclose(file);

    fclose (FileVarriable);
    return 0;
}

Note

FileVarriable– It is a variable which open file for reading/writing etc..

FileLocation– This variable keeps the file location.

fprintf function- sends formatted output to a stream.

fopen (FileLocation,”w+“)- Here w+ is the mode, which instruct the data to follow action accordingly.

“r” –Opens a file for reading. The file must exist.
“w” –Creates an empty file for writing. If a file with the same name already exists, its content is erased and the file is considered as a new empty file.
“a” –Appends to a file. Writing operations, append data at the end of the file. The file is created if it does not exist.
“r+” –Opens a file to update both reading and writing. The file must exist.
“w+” –Creates an empty file for both reading and writing.
“a+” –Opens a file for reading and appending.

No comments:

Post a Comment