Write a small program that copies data from a file A to a file B. For different
ID: 3844947 • Letter: W
Question
Write a small program that copies data from a file A to a file B. For different file sizes 1KB – 100MB (doubling the file size in each step) record the time your program requires to complete the copy when using read() and write() system calls for each byte that is being copied. Generate a graph that depicts the program performance. Briefly describe what you observe and investigate if you can improve the program’s performance. For this program it is imperative that file A and file B are located on the local disk. On Linux, the mp directory is located on the local file system. After completing the assignment with the read/write system calls, use the C Library functions fread() and fwrite() as replacements. Include the performance using these functions in your graph, and discuss the differences (if any) you observe. Speculate on the reason for any differences. For submission,
a. Provide your source code in a file named copying.cpp
b. Provide your graph in a file named copying.jpg
c. Provide your description in a file named copying.txt
Explanation / Answer
copying.cpp
#include <iostream>
#include <fstream>
#include <ctime>
using namespace std;
int main(){
int size = 1000;
ifstream a("dummies/fileA.txt", ifstream::in);
ofstream b("dummies/fileB.txt", ofstream::out);
ofstream c("copying.txt", ofstream::out);
c << " ++++++ fwrite() & fread() ++++++ ";
while (!a.eof() || size <= 100000000){
clock_t begin = clock();
char* buffer = new char[size];
a.fread(buffer, size); //reading from file A
b.fwrite(buffer, size); //writing to file B
cout << size/1000 << " KB" << endl;
size *= 2;
delete[] buffer;
clock_t end = clock();
double elapsed_secs = double(end - begin) / CLOCKS_PER_SEC;
c << size/1000 << "KB to0k: " << elapsed_secs << endl << endl;
}
a.close();
b.close();
c.close();
return 0;
}
#include <iostream>
#include <stdio.h>
#include <fstream>
#include <stdlib.h>
#include <ctime>
using namespace std;
int main(){
int size = 1000;
size_t result;
ofstream c;
FILE * a;
FILE * b;
c.open("copying.txt", ofstream::out | ofstream::app);
c << " ++++++ fwrite() & fread() ++++++ ";
while (size <= 100000000){
clock_t begin = clock();
a = fopen("dummies/fileA.txt", "r" );
b = fopen("dummies/fileB.txt", "w");
if (a == NULL || b == NULL){fputs ("File error",stderr); exit (1);}
char* buffer = (char*)malloc (sizeof(char)*size);
if (buffer == NULL) {fputs ("Memory error",stderr); exit (2);}
result = fread(buffer, 1, size, a);
//if (result != size) {fputs ("Reading error",stderr); exit (3);}
result = fwrite(buffer, 1, size, b);
//if (result != size) {fputs ("Reading error",stderr); exit (4);}
cout << size/1000 << " KB" << endl;
size *= 2;
free (buffer);
clock_t end = clock();
double elapsed_secs = double(end - begin) / CLOCKS_PER_SEC;
c << size/1000 << "KB took: " << elapsed_secs << endl << endl;
}
fclose(a);
fclose(b);
c.close();
return 0;
}
copying.txt
++++++ write() & read() ++++++
2KB took: 0
4KB took: 0
8KB took: 0
16KB took: 0
32KB took: 0
64KB took: 0.001
128KB took: 0
256KB took: 0
512KB took: 0.001
1024KB took: 0.002
2048KB took: 0.004
4096KB took: 0.02
8192KB took: 0.018
16384KB took: 0.03
32768KB took: 0.044
65536KB took: 0.136
131072KB took: 0.217
++++++ fwrite() & fread() ++++++
2KB took: 0
4KB took: 0.001
8KB took: 0
16KB took: 0
32KB took: 0.001
64KB took: 0
128KB took: 0.001
256KB took: 0.001
512KB took: 0.001
1024KB took: 0.002
2048KB took: 0.002
4096KB took: 0.006
8192KB took: 0.011
16384KB took: 0.018
32768KB took: 0.037
65536KB took: 0.092
131072KB took: 0.176
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.