-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmoveFile.cpp
More file actions
60 lines (52 loc) · 1.49 KB
/
moveFile.cpp
File metadata and controls
60 lines (52 loc) · 1.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
bool transferFile(const std::string& sourceFolder, const std::string& targetFolder, const std::string& fileName)
{
// Open the source file for reading
ifstream sourceFile(sourceFolder + "/" + fileName, std::ios::binary);
if (!sourceFile)
{
cout << "Error: Failed to open source file " << fileName << std::endl;
return false;
}
// Open the target file for writing
ofstream targetFile(targetFolder + "/" + fileName, std::ios::binary);
if (!targetFile)
{
cout << "Error: Failed to open target file " << fileName << std::endl;
return false;
}
// Transfer the file contents
targetFile << sourceFile.rdbuf();
// Check for any errors during the transfer
if (sourceFile.bad() || targetFile.bad())
{
cout << "Error: Failed to transfer file " << fileName << std::endl;
return false;
}
cout << "File transfer successful: " << fileName << std::endl;
return true;
}
int main()
{
string sourceFolder;
cout<<"enter path of source folder:"<<endl;
cin>>sourceFolder;
string targetFolder;
cout<<"enter path of target folder:"<<endl;
cin>>targetFolder;
string fileName;
cout<<"enter file name : "<<endl;
cin>>fileName;
if (transferFile(sourceFolder, targetFolder, fileName))
{
cout<<" File transfer successful"<<endl;
}
else
{
cout<<" File transfer failed "<<endl;
}
return 0;
}