-
Notifications
You must be signed in to change notification settings - Fork 5
/
FileMaking.cpp
executable file
·266 lines (163 loc) · 4.9 KB
/
FileMaking.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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
#ifndef __FileMaking
#define __FileMaking
#include<iostream>
#include<fstream>
#include<string>
#include<cstdlib>
#include<sstream>
#include <iostream>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <stdio.h>
#include<sstream>
#include<fstream>
#include<inttypes.h>
#include <cstdlib>
#include <sys/timeb.h>
#include <sys/stat.h>
#include <math.h> /* floor */
using namespace std;
// template to convert Number to string
template<typename T>
string NumberToString(T Number) {
stringstream ss;
ss << Number;
return ss.str();
}
// templates end
// Simply gets the file size of file.
int getFileSize(ifstream *file) {
file->seekg(0, ios::end);
int filesize = file->tellg();
file->seekg(ios::beg);
return filesize;
}
////////////////////////////////////// makes file chunks
void chunkFile(char* fullFilePath, char *chunkName, int no_of_chunks,string destinationFolder) {
const int dir_err = mkdir(destinationFolder.c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
ifstream fileStream, s;
unsigned long chunkSize;
s.open(fullFilePath);
int size = getFileSize(&s);
cout << "Size is : " << size << endl;
chunkSize = ceil(size / no_of_chunks); // no of chunks
cout << "Size is : " << chunkSize << endl;
chunkSize--;
fileStream.open(fullFilePath, ios::in | ios::binary);
// File open a success
if (fileStream.is_open()) {
ofstream output;
int counter = 1;
string fullChunkName;
// Create a buffer to hold each chunk
char *buffer = new char[chunkSize];
// Keep reading until end of file
while (!fileStream.eof()) {
// Build the chunk file name. Usually drive:\\chunkName.ext.N
// N represents the Nth chunk
fullChunkName.clear();
fullChunkName.append(destinationFolder+"//");
fullChunkName.append(chunkName);
fullChunkName.append(".");
//cout<<fullChunkName;
// Convert counter integer into string and append to name.
// char intBuf[10];
string str = NumberToString(counter);
// itoa(counter,intBuf,10);
fullChunkName.append(str);
// Open new chunk file name for output
output.open(fullChunkName.c_str(),
ios::out | ios::trunc | ios::binary);
// If chunk file opened successfully, read from input and
// write to output chunk. Then close.
if (output.is_open()) {
fileStream.read(buffer, chunkSize);
// gcount() returns number of bytes read from stream.
output.write(buffer, fileStream.gcount());
output.close();
counter++;
}
}
// Cleanup buffer
delete (buffer);
// Close input file stream.
fileStream.close();
cout << "Chunking complete! " << counter - 1 << " files created."
<< endl;
}
else {
cout << "Error opening file!" << endl;
}
} // chunkfile function end here
///////////////////////////////////////////////////////////////////////
// Finds chunks by "chunkName" and creates file specified in fileOutput
void joinFile(char *chunkName, char *fileOutput) {
string fileName;
// Create our output file
ofstream outputfile;
outputfile.open(fileOutput, ios::out | ios::binary);
// If successful, loop through chunks matching chunkName
if (outputfile.is_open()) {
bool filefound = true;
int counter = 1;
int fileSize = 0;
while (filefound) {
filefound = false;
// Build the filename
fileName.clear();
fileName.append(chunkName);
fileName.append(".");
char intBuf[10];
string str = NumberToString(counter);
fileName.append(str);
// Open chunk to read
ifstream fileInput;
fileInput.open(fileName.c_str(), ios::in | ios::binary);
// If chunk opened successfully, read it and write it to
// output file.
if (fileInput.is_open()) {
filefound = true;
fileSize = getFileSize(&fileInput);
char *inputBuffer = new char[fileSize];
fileInput.read(inputBuffer, fileSize);
outputfile.write(inputBuffer, fileSize);
delete (inputBuffer);
fileInput.close();
}
counter++;
}
// Close output file.
outputfile.close();
cout << "File assembly complete!" << endl;
}
else {
cout << "Error: Unable to open file for output." << endl;
}
}
/*
int main() {
char *path, *name, *combined_files;
string filepath = "./A.txt"; // yahan par file ka path aur location day
string filename = "B"; // yahan par aik name day jis name say parts banay gay
string joined_files = "joind_files"; // ye us file ka name hai jo join honay k bad bnay gay
path = &filepath[0];
name = &filename[0];
combined_files = &joined_files[0];
chunkFile(path, name, 5,"N"); // is mein path file ka path ha
// name chunk ka name ha jis name par tu nay create karna ha
// 5 no of chunk files hain
joinFile(name, combined_files);
//cout<<udp_checksum(path, 15);
string newFolder;
cout << "Enter name of new project without spaces:\n";
cin >> newFolder;
return 0;
}
*/
#endif