ownaginatious
June 1st, 2010, 06:41 AM
Now before anyone tells me something like this already exists, this is purely an experiment to help me learn C ;).
Anyway, I got bored and decided to try and make a C program that works very similarly to "cp" in Unix, except that it also shows me a progress bar when I'm copying a file.
Right now the only way to copy a file from one place to another is by hardcoding the paths in.
It seems to work, but the copy speed is relatively slow.
Does anyone know any tips on how I can make the copying faster?
Thanks!
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
long fileSize(FILE* input_file);
void copy_file(char* from_path, char* to_path);
int main(){
char * file1 = "/path/to/existing/file";
char * file2 = "/path/to/copy/to";
copy_file(file1, file2);
}
long fileSize(FILE* input_file){
long size;
fseek(input_file, 0, SEEK_END);
size = ftell(input_file);
fseek(input_file, 0, SEEK_SET);
return size;
}
// Copys one file from one location to another
void copy_file(char* from_path, char* to_path){
FILE * old_file = fopen(from_path, "rb");
FILE * new_file = fopen(to_path, "wb");
long totalSize = fileSize(old_file);
int percent = 0;
double last = 0;
double speed = 0;
long count = 0, difference = 0, lastTime = 0, lastCount = 0;
int i = 0;
time_t timer = time(NULL);
while(!feof(old_file)){
timer = time(NULL);
percent = (int)(100 * count++/totalSize);
difference = timer - lastTime;
if(difference >= 1){
speed = count - lastCount;
lastCount = count;
lastTime = timer;
fflush(stdout);
last = percent;
printf("%d% |", percent);
for(i = 0; i < 10; i++)
if(i < (int)percent/10)
printf("*");
else
printf("-");
printf("| [%f MB/s]\r", speed/(1024*1024));
}
putc(fgetc(old_file), new_file);
}
printf("\n");
fclose(old_file);
fclose(new_file);
}
Anyway, I got bored and decided to try and make a C program that works very similarly to "cp" in Unix, except that it also shows me a progress bar when I'm copying a file.
Right now the only way to copy a file from one place to another is by hardcoding the paths in.
It seems to work, but the copy speed is relatively slow.
Does anyone know any tips on how I can make the copying faster?
Thanks!
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
long fileSize(FILE* input_file);
void copy_file(char* from_path, char* to_path);
int main(){
char * file1 = "/path/to/existing/file";
char * file2 = "/path/to/copy/to";
copy_file(file1, file2);
}
long fileSize(FILE* input_file){
long size;
fseek(input_file, 0, SEEK_END);
size = ftell(input_file);
fseek(input_file, 0, SEEK_SET);
return size;
}
// Copys one file from one location to another
void copy_file(char* from_path, char* to_path){
FILE * old_file = fopen(from_path, "rb");
FILE * new_file = fopen(to_path, "wb");
long totalSize = fileSize(old_file);
int percent = 0;
double last = 0;
double speed = 0;
long count = 0, difference = 0, lastTime = 0, lastCount = 0;
int i = 0;
time_t timer = time(NULL);
while(!feof(old_file)){
timer = time(NULL);
percent = (int)(100 * count++/totalSize);
difference = timer - lastTime;
if(difference >= 1){
speed = count - lastCount;
lastCount = count;
lastTime = timer;
fflush(stdout);
last = percent;
printf("%d% |", percent);
for(i = 0; i < 10; i++)
if(i < (int)percent/10)
printf("*");
else
printf("-");
printf("| [%f MB/s]\r", speed/(1024*1024));
}
putc(fgetc(old_file), new_file);
}
printf("\n");
fclose(old_file);
fclose(new_file);
}