Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Copying Files in C++(SHFileOperation )

Could someone please help me on this, I would appreciate it very much.
I have checked out the documentation on MSDN for Copying files in C++. The win api function I am using works properly when I use string literals for the source and destination e.g. "C:\Windows...." However, when I copy a string into a char array using them as the source and destination, then execute the program, I get an Error message saying"file can not be copied and disk can not be read". I have enclosed my code if anyone can help or offer better suggestions for what I am trying to do.


#include
#include
#include
#include
#include
#include
#include
using namespace std;


int main()

{

string file;
string file1;
string file2;
string file3;
string file4;
string file5;
string file6;
string Path;




cout<<" UTILITY TO TRANSFER FILE FROM SERVER"<<endl;
cout<<endl;
cout<<"PLEASE ENTER PROGRAM FILE NAME: ";
cin>>file;

//gets certain characters from input string to create path for user //entered file
file1 = file.substr(0,1);
file2 = file.substr(0,2);
file3 = file.substr(0,3);
file4 = file.substr(1,1);
file5 = file.substr(1,2);
file6 = file.substr(1,3);


//preparing input for validation
char alpha[100];
int s;

s = file.size();

file.copy(alpha,s);



if(isalpha(alpha[0]))//if file begins with letter skip to second

{


Path = "H:\"+ file4 + "\" + file5 + "\" + file6 + "00\" + file + ".txt" + "";
}


else // file begins with integer start at first char
{

Path = "H:\"+ file1 + "\" + file2 + "\" + file3 + "00\" + file + ".txt" + "";

}




string Path2 = "H:\Work\text.txt";//destination for all copies


char src [100];
char dst [100];

int i;
int j;

i = Path.size();//get size of source path
j = Path2.size();//get size of dest path


Path.copy(src,i); //copy path into char array
Path2.copy(dst,j);//copy path2 into char array



SHFILEOPSTRUCTA fop;


// initialize all data required for the copy
fop.hwnd = NULL;
fop.wFunc = FO_COPY;
fop.pFrom = src; //src is a char array should be valid but
fop.pTo = dst;//dst is a char array should be valid but
fop.fFlags = FOF_NOCONFIRMATION ;

if(!SHFileOperation(&fop)) //performs copy only if path exist
{
cout<<"File is Transferring";
cout<<endl;

}


else
{
cout<<endl;
cout<<"File Not Found!!!";
cout<<endl;

}



}

Comments

  • tsagldtsagld Member Posts: 621
    : Could someone please help me on this, I would appreciate it very
    : much.
    : I have checked out the documentation on MSDN for Copying files in
    : C++. The win api function I am using works properly when I use
    : string literals for the source and destination e.g.
    : "C:\Windows...." However, when I copy a string into a char array
    : using them as the source and destination, then execute the program,
    : I get an Error message saying"file can not be copied and disk can
    : not be read". I have enclosed my code if anyone can help or offer
    : better suggestions for what I am trying to do.
    :
    :
    : #include
    : #include
    : #include
    : #include
    : #include
    : #include
    : #include
    : using namespace std;
    :
    :
    : int main()
    :
    : {
    :
    : string file;
    : string file1;
    : string file2;
    : string file3;
    : string file4;
    : string file5;
    : string file6;
    : string Path;
    :
    :
    :
    :
    : cout<<" UTILITY TO TRANSFER FILE FROM
    : SERVER"<<endl;
    : cout<<endl;
    : cout<<"PLEASE ENTER PROGRAM FILE NAME: ";
    : cin>>file;
    :
    : //gets certain characters from input string to create path for user
    : //entered file
    : file1 = file.substr(0,1);
    : file2 = file.substr(0,2);
    : file3 = file.substr(0,3);
    : file4 = file.substr(1,1);
    : file5 = file.substr(1,2);
    : file6 = file.substr(1,3);
    :
    :
    : //preparing input for validation
    : char alpha[100];
    : int s;
    :
    : s = file.size();
    :
    : file.copy(alpha,s);
    :
    :
    :
    : if(isalpha(alpha[0]))//if file begins with letter skip to second
    :
    : {
    :
    :
    : Path = "H:\"+ file4 + "\" + file5 + "\" + file6 + "00\"
    : + file + ".txt" + "";
    : }
    :
    :
    : else // file begins with integer start at first char
    : {
    :
    : Path = "H:\"+ file1 + "\" + file2 + "\" + file3 + "00\"
    : + file + ".txt" + "";
    :
    : }
    :
    :
    :
    :
    : string Path2 = "H:\Work\text.txt";//destination for all copies
    :
    :
    : char src [100];
    : char dst [100];
    :
    : int i;
    : int j;
    :
    : i = Path.size();//get size of source path
    : j = Path2.size();//get size of dest path
    :
    :
    : Path.copy(src,i); //copy path into char array
    : Path2.copy(dst,j);//copy path2 into char array
    :
    :
    :
    : SHFILEOPSTRUCTA fop;
    :
    :
    : // initialize all data required for the copy
    : fop.hwnd = NULL;
    : fop.wFunc = FO_COPY;
    : fop.pFrom = src; //src is a char array should be valid but
    : fop.pTo = dst;//dst is a char array should be valid but
    : fop.fFlags = FOF_NOCONFIRMATION ;
    :
    : if(!SHFileOperation(&fop)) //performs copy only if path exist
    : {
    : cout<<"File is Transferring";
    : cout<<endl;
    :
    : }
    :
    :
    : else
    : {
    : cout<<endl;
    : cout<<"File Not Found!!!";
    : cout<<endl;
    :
    : }
    :
    :
    :
    : }
    :
    Probably too late, but the pTo-parameter should also be double-NULL terminated, just like pFrom.

    Greets,
    Eric Goldstein
    http://www.gollie.nl
Sign In or Register to comment.