943,778 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 1340
  • C RSS
Jan 5th, 2009
-1

can't copy existing file into new file

Expand Post »
this coding is about copy one existing file into other file

but it can't do so throught the command line arg. what is

the solution....?
  1. #include<stdio.h>
  2.  
  3. #include<conio.h>
  4.  
  5. #include<stdlib.h>
  6.  
  7. void main(char *argv[])
  8. {
  9.  
  10. FILE *fp1,*fp2;
  11. char c;
  12. int i;
  13.  
  14. clrscr();
  15.  
  16. fp1=fopen(argv[1],"r");
  17.  
  18. rewind(fp1);
  19.  
  20. fp2=fopen(argv[2],"w");
  21.  
  22.  
  23. if(fp1==NULL)
  24. {
  25.  
  26. printf("Source File is Not Exist");
  27. exit(0);
  28.  
  29. }
  30.  
  31.  
  32.  
  33. while((c=getc(fp1))!=EOF)
  34. {
  35.  
  36.  
  37. putc(c,fp2);
  38.  
  39. printf("%c",c);
  40.  
  41.  
  42. }
  43.  
  44. fclose(fp1);
  45.  
  46. fclose(fp2);
  47.  
  48. getch();
  49.  
  50. }
Last edited by Ancient Dragon; Jan 5th, 2009 at 9:39 pm. Reason: add code tags
Similar Threads
Reputation Points: 0
Solved Threads: 1
Light Poster
nitu_thakkar is offline Offline
25 posts
since Jan 2009
Jan 5th, 2009
0

Re: can't copy existing file into new file

1. Wrong main prototype. Must be:
  1. int main(int argc, char*argv[])
2. fgetc returns int, declare c as int.
3. No need in rewind(fp1). Test fp1 == 0 befory any ops with opened file.
4. Test argc == 3 (argv[0] - program name, argv[1] and argv[2] - file names).
5. You can't copy binary file in text mode. To open in binary mode use "rb" and "wb" open mode arguments.
Last edited by ArkM; Jan 5th, 2009 at 7:01 pm.
Reputation Points: 1234
Solved Threads: 347
Postaholic
ArkM is offline Offline
2,001 posts
since Jul 2008
Jan 6th, 2009
0

Re: can't copy existing file into new file

Click to Expand / Collapse  Quote originally posted by ArkM ...
1. Wrong main prototype. Must be:
  1. int main(int argc, char*argv[])
2. fgetc returns int, declare c as int.
3. No need in rewind(fp1). Test fp1 == 0 befory any ops with opened file.
4. Test argc == 3 (argv[0] - program name, argv[1] and argv[2] - file names).
5. You can't copy binary file in text mode. To open in binary mode use "rb" and "wb" open mode arguments.

i have tried it but there is no change i got the same massage

here, getc() function used for get the value from existing file ,

character by character & put into new file

( which is given during run time )

character by character

can u pls send me more detail....?
Last edited by nitu_thakkar; Jan 6th, 2009 at 1:28 pm.
Reputation Points: 0
Solved Threads: 1
Light Poster
nitu_thakkar is offline Offline
25 posts
since Jan 2009
Jan 6th, 2009
0

Re: can't copy existing file into new file

>>i have tried it but there is no change i got the same massage
What error message -- you never said what that was. Post code and make sure main() is declared like ArkM posted.

Post code if you need more help. And if you are trying to copy binary files then you have to open the files in binary mode, also as previously suggested.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,950 posts
since Aug 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: Unable to open file with fopen
Next Thread in C Forum Timeline: Append a Character(backslash) to a string at any position





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC