Samar,
If you please allow me to suggest some improvements:
1. if((inFilePtr=fopen(argv[1],"r"))!=NULL)
Not necessary to check the inside of an if against "different from NULL" in any
occasion.
if(inFilePtr=fopen(argv[1],"r")) suffices.
As a corollary, you save the typing of a pair of "()" and make your code
clearer and cleaner.
2. if(outFilePtr=fopen(argv[2], "w")) != NULL)
Ditto.
if(outFilePtr=fopen(argv[2], "w")) suffices.
3. c=fgetc(inFilePtr)
Reading one byte per cycle is too much time consuming.
Better policy is to use a buffer.
char buffer[1024];
count = fread(b, 1, 1024, inFilePtr);
or
fgets(buffer, 1024, inFilePtr);
Working with a buffer in RAM is much faster than making thousands of successive
calls to fgetc() to read your file for the same purpose and in case you're
reading from an electromechanical device such as a diskette or an HD the
economy in time is even more apparent.
The gain is dramatic, believe me: I've tested it many times.
The larger is the file the greater is the gain.
4. fputc(c, outFilePtr)
Ditto, but for writing.
char buffer[1024];
fwrite(buffer, 1, count, outFilePtr);
or
fputs(buffer, outFilePtr);
5. Presto and here's your source code for transferring even-numbered lines from
one file to another made briefer/simpler.
Text lines from text files, well understood, because this concept don't apply
to binary files.
Would you change your mind and prefer the odd-numbered lines, change int line=0
to int line=1;
#include <stdio.h>
int main(int argc, char *argv[]) {
if (argc!=3) {puts("Wrong number of arguments!"); return 0;}
FILE *i, *o;
i=fopen(argv[1], "r");
if (!i) {puts("Wrong input file!"); return 0;}
o=fopen(argv[2], "w");
if (!o) {puts("Wrong output file!"); return 0;}
char buffer[1024];
int line=0;
while (fgets(buffer, 1024, i)) {
if (line) fputs(buffer, o);
line=!line;
}
return 0;
}
Happy coding!
Geraldo
--- In [email protected], samar aseeri <samarase...@...> wrote:
>
> hi,
>
> the below attached program uses the argument command line the copy a file to
> another file. my question is what can I change in the program to make only
> part of the first file be copied to the second.
>
> #include<stdio.h>
> int main(int argc, char *argv[])
> {
> FILE *inFilePtr; /*input file pointer*/
> FILE *outFilePtr; /*output fole pointer*/
> int c,i;
> /*check number of command-line arguments */
> if(argc != 3){
> printf("Usage: mycopy infile outfile\n");} /*end if*/
> else /*if input file can be opened*/
> {
> if((inFilePtr=fopen(argv[1],"r"))!=NULL) /*if input file can be
> opened*/
> { if((outFilePtr =fopen(argv[2], "w")) != NULL) /*if output file can
> be opened*/
> {while((c = fgetc(inFilePtr)) !=EOF) /*read and output
> characters*/
> {
> fputc(c,outFilePtr);}/*end while*/
> }/*end if*/
> else /*output file could not be opened*/
> {printf("File\"%s\" could not be opened\n", argv[2]);
> } /*end else*/
> }/*end if*/
> else /*input file could not be opened*/
> { printf("File\"%s\" coulk not be opened\n", argv[1]);} /*end else*/
> } /*end else*/
> return 0; /*indicates successful termination*/
> }/*end main*/
>
>
>
> Get your preferred Email name!
> Now you can @ymail.com and @rocketmail.com.
> http://mail.promotions.yahoo.com/newdomains/aa/
>
> [Non-text portions of this message have been removed]
>