ozkaneren
asked on
file copying in C
hello I am looking how to copy a file in C
I mean
like this :
in ms dos prompt I will prompt
program.exe 1.dat 2.dat
and 1.dat will be copied to 2.dat ( I dont know how to use argc argv , I would appreciate if you can explain this)
I mean
like this :
in ms dos prompt I will prompt
program.exe 1.dat 2.dat
and 1.dat will be copied to 2.dat ( I dont know how to use argc argv , I would appreciate if you can explain this)
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
#include <stdio.h>
#define buflen 1000
char buffer[buflen];
int main( int argc, char *argv[] ){
FILE *F,*T;
int n;
if( argc != 3 ){
printf("usage: %s from to\n",argv[0]);
exit(1);
}
if( !(F=fopen(argv[1],"r")) ){
perror(argv[1]);
exit(1);
}
if( !(T=fopen(argv[2],"w")) ){
perror(argv[2]);
exit(1);
}
while( n = fread(buffer,1,buflen,F) ){
fwrite(buffer,1,n,T);
}
}
#define buflen 1000
char buffer[buflen];
int main( int argc, char *argv[] ){
FILE *F,*T;
int n;
if( argc != 3 ){
printf("usage: %s from to\n",argv[0]);
exit(1);
}
if( !(F=fopen(argv[1],"r")) ){
perror(argv[1]);
exit(1);
}
if( !(T=fopen(argv[2],"w")) ){
perror(argv[2]);
exit(1);
}
while( n = fread(buffer,1,buflen,F) ){
fwrite(buffer,1,n,T);
}
}
> #define buflen 1000
> char buffer[buflen];
Disk sectors are 512 bytes in size, so it is most efficient to read
data in multiples of 512. So you would be better off with buflen of 1024
than 1000. Even such a tiny buffer is inefficient for disk copying.
Try buflen of 16384.
> char buffer[buflen];
Disk sectors are 512 bytes in size, so it is most efficient to read
data in multiples of 512. So you would be better off with buflen of 1024
than 1000. Even such a tiny buffer is inefficient for disk copying.
Try buflen of 16384.
But the stdio library takes care of matching buffer sizes to file systems' preferred block sizes, so the buffer size you use in a stdio-using program makes no difference in the size and alignment of the read and write transactions with the actual disk device.
But I, too, would have chosen a larger buffer size.
And, if you're looking for efficiency, I'd probably forego the use of stdio calls entirely.
But I, too, would have chosen a larger buffer size.
And, if you're looking for efficiency, I'd probably forego the use of stdio calls entirely.
> And, if you're looking for efficiency, I'd probably forego the use of stdio calls entirely.
Yeah, for strictly large block reads & writes, the buffered io in fopen(), et al provides no benefit.
I prefer to use open() et al, instead.
Yeah, for strictly large block reads & writes, the buffered io in fopen(), et al provides no benefit.
I prefer to use open() et al, instead.
brettmjohnson:
you're right about that. Thanks for correcting me. As i said it's been a while so a little refreshing is always great.
you're right about that. Thanks for correcting me. As i said it's been a while so a little refreshing is always great.
if you are on windows, the most efficient way to copy a file would be to use
#include <windows.h>
and
BOOL CopyFile(
LPCTSTR lpExistingFileName,
LPCTSTR lpNewFileName,
BOOL bFailIfExists
);
then the os does it for you..
#include <windows.h>
and
BOOL CopyFile(
LPCTSTR lpExistingFileName,
LPCTSTR lpNewFileName,
BOOL bFailIfExists
);
then the os does it for you..
program.exe 1.dat 2.dat
Then the main() routine of program.exe will receive argc, and argv as follows:
argc = 3
argv[0] = "program.exe"
argv[1] = "1.dat"
argv[2] = "2.dat"