-
Notifications
You must be signed in to change notification settings - Fork 1
/
File_copy_with_multi_thread.cpp
183 lines (158 loc) · 4.94 KB
/
File_copy_with_multi_thread.cpp
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
// author: jaydattpatel
/*
copy file
These internal stream positions point to the locations within the stream where the next reading or writing operation is performed. These positions can be observed and modified using the following member functions:
tellg()
tellp()
These two member functions with no parameters return a value of the member type streampos, which is a type representing the current get position (in the case of tellg) or the put position (in the case of tellp).
seekg ( position );
seekp ( position );
seekg ( offset, direction );
seekp ( offset, direction );
ios::beg offset counted from the beginning of the stream
ios::cur offset counted from the current position
ios::end offset counted from the end of the stream
streampos - ios::pos_type - Defined as fpos<mbstate_t>.
It can be converted to/from streamoff and can be added or subtracted values of these types.
streamoff - ios::off_type - It is an alias of one of the fundamental integral types (such as int or long long).
*/
// obtaining ifile size
#include <iostream>
#include <fstream>
#include <iomanip>
#include <thread>
#include <math.h>
using namespace std;
#define clear() printf("\033[H\033[J")
#define gotoxy(x,y) printf("\033[%d;%dH", (x), (y))
unsigned long long copied_size = 0;
bool open = false, finished = false;
int sam = 0;
bool running = false;
void sizeprint()
{
gotoxy(5,30);
if(copied_size<pow(1024,0) && copied_size<pow(1024,1))
cout << "Copied: " <<setw(6)<<setfill(' ')<<left<< copied_size << " bytes.";
else if(copied_size>=pow(1024,1) && copied_size<pow(1024,2))
cout << "Copied: " <<setw(6)<<setfill(' ')<<left<< (copied_size/pow(1024,1)) << " KB.";
else if(copied_size>=pow(1024,2) && copied_size<pow(1024,3))
cout << "Copied: " <<setw(6)<<setfill(' ')<<left<< (copied_size/pow(1024,2)) << " MB.";
else if(copied_size>=pow(1024,3))
cout << "Copied: " <<setw(6)<<setfill(' ')<<left<< (copied_size/pow(1024,3)) << " GB.";
}
void filecopy(char *source, char *destination)
{
ifstream ifile;
ifile.open(source, ios::binary | ios::in);
if(ifile.is_open())
cout<<endl<<source<<" File opened successfully to read.";
else
{
cout<<endl<<source<<" File Error to Open File !!!";
return;
}
ofstream ofile;
ofile.open(destination, ios::binary| ios::out);
if(ofile.is_open())
cout<<endl<<destination<<" File opened successfully to write.";
else
{
cout<<endl<<destination<<" File Error to Open File !!!";
return;
}
ifile.seekg (0, ios::beg);
ofile.seekp (0, ios::beg);
cout<<endl<<"File Copying........\nPlease Wait";
open = true;
char buff[1024];
while (ifile.read(buff, sizeof(buff)))
{
copied_size = ifile.gcount() + copied_size;
ofile.write(buff, ifile.gcount());
}
ifile.close();
ofile.close();
open = false;
finished = true;
sizeprint();
cout<<endl<<"..........Finished";
}
void sizeupdate()
{
while(1)
{
while(open)
{
if(sam == 0 && !running)
{
running = true;
gotoxy(5,30);
cout<<" ";
sizeprint();
sam++;
running = false;
this_thread::sleep_for(250ms);
}
}
if(finished)
break;
}
}
void dotanimation()
{
while(1)
{
while(open)
{
static int i = 0;
if(sam == 1 && !running)
{
running = true;
if (i > 10)
{
gotoxy(5,12);
for(int j = 0; j<=10; j++)
cout<<" ";
i = 0;
}
else
{
gotoxy(5,i+12);
cout<<"." ;
i++;
}
sam--;
running = false;
this_thread::sleep_for(500ms);
}
}
if(finished)
break;
}
}
void samaphore()
{
while(open)
{
while(sam<1)
sam++;
while(sam>0)
sam--;
}
}
int main()
{
clear();
char source[] = {"Lam.jpg"};
char destination[] = {"Mla.jpg"};
thread t1(filecopy,source,destination);
thread t2(sizeupdate);
thread t3(dotanimation);
thread t4(samaphore);
t1.join();
t2.join();
t3.join();
t4.join();
return 0;
}