Solved

file copying in C

Posted on 2003-10-22
9
324 Views
Last Modified: 2010-04-17
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)  
0
Comment
Question by:ozkaneren
9 Comments
 

Accepted Solution

by:
theMuzz earned 125 total points
ID: 9601636
/* argc will give you the number of arguments you've passed
   argv[] returns the values for each argument

int main(int argc, char* argv[])
{
      int numberOfArguments;
      numberOfArguments = argc;

      string arg1, arg2, arg3; // and so on

      arg1 = argv[0];
      arg2 = argv[1];
      arg3 = argv[2];
}

Can't remember if first argument is argv[0] or argv[1], I'm pretty sure it's 0.


so... program.exe three small arguments
would result in

arg1 = 'three'
arg2 = 'small'
arg3 = 'arguments'

Hope this helps
0
 
LVL 23

Expert Comment

by:brettmjohnson
ID: 9602623
If you invoke the program as follows:

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"

0
 
LVL 84

Expert Comment

by:ozo
ID: 9602797
#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);
  }
}
0
 
LVL 23

Expert Comment

by:brettmjohnson
ID: 9602958
> #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.


0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 20

Expert Comment

by:jmcg
ID: 9604129
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.
0
 
LVL 23

Expert Comment

by:brettmjohnson
ID: 9604375
> 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.
0
 

Expert Comment

by:theMuzz
ID: 9605941
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.
0
 
LVL 5

Expert Comment

by:drnick
ID: 9612406
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..
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Specific format 21 185
File.WriteAllLines problem at random C# ASP.NET 6 66
delphi parse string to params 3 101
recursion example 16 112
RIA (Rich Internet Application) tools are interactive internet applications which have many of the characteristics of desktop applications. The RIA tools typically deliver output either by the way of a site-specific browser or via browser plug-in. T…
This is about my first experience with programming Arduino.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now