samar aseeri <samarase...@...> wrote:
Subject: samar
Please make your subject line describe the nature of your
post. Your name is far from a good description of the content.
> 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.
Before you change the code, you first have to decide what
part you want to return. Is it a number of characters, a
a number of words, a numbr of lines, only every third
palindrome, ... what?
> #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*/
These messages are better off directed to stderr.
> 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*/
That's your core loop. What you need is...
while (...keep going condition... && (c = fgetc(inFilePtr)) !=EOF)
> {
> 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*/
ITYM could, not coulk.
> } /*end else*/
> return 0; /*indicates successful termination*/
Which would be an ironic thing to return if it couldn't open
either stream.
> }/*end main*/
--
Peter