943,903 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 4689
  • C RSS
You are currently viewing page 1 of this multi-page discussion thread
Oct 1st, 2006
0

Reading and Writing file

Expand Post »
Can anyone help me...i have a file modt.txt and i want to save the content in modt2.txt but it seems my code is not working. Moreover,
if i have the content of the 1st file Have a nice day. i want to save it to the 2nd file without double space it should be Have a nice day. I have this as my code:

  1. #include <stdio.h>
  2.  
  3. void copyf(char *fname1, char *fname2){
  4. FILE *fp1,*fp2;
  5. int x;
  6.  
  7. if (((fp1=fopen(fname1,"r"))==NULL)||((fp2=fopen(fname2,"w"))==NULL))
  8. {
  9. printf("error:unable to open the filed\n");
  10. exit(1);
  11. }
  12. while((x=getc(fp1))!=EOF)
  13. putc(x,fp2);
  14. fclose(fp1);
  15. fclose(fp1);
  16. }
  17. main(){
  18.  
  19. copyf("modt.txt","modt2.txt");
  20.  
  21. }
Similar Threads
Reputation Points: 16
Solved Threads: 0
Light Poster
vbcielle is offline Offline
26 posts
since Sep 2006
Oct 2nd, 2006
0

Re: Reading and Writing file

don't understand your problem. In modt.txt you have Have a nice day and in modt2.txt I also have the same string.
>i want to save it to the 2nd file without double space
This part I don't understand. Where you have double space? In 2nd file?
Reputation Points: 251
Solved Threads: 29
Posting Whiz in Training
andor is offline Offline
274 posts
since Jun 2005
Oct 2nd, 2006
0

Re: Reading and Writing file

Click to Expand / Collapse  Quote originally posted by andor ...
don't understand your problem. In modt.txt you have Have a nice day and in modt2.txt I also have the same string.
>i want to save it to the 2nd file without double space
This part I don't understand. Where you have double space? In 2nd file?
im sory the first txt file should have a content of "Have_ _ a nice day" two spaces then i have to save it to the 2nd file and should only have one spcae "Have_a nice day"...
Reputation Points: 16
Solved Threads: 0
Light Poster
vbcielle is offline Offline
26 posts
since Sep 2006
Oct 2nd, 2006
0

Re: Reading and Writing file

  1. while((x=getc(fp1))!=EOF){
  2. if ((unsigned char) x != ' ' || spaceOcured != 1)
  3. putc(x,fp2);
  4. if ((unsigned char) x == ' '){
  5. spaceOcured = 1;
  6. }
  7. else{
  8. spaceOcured = 0;
  9. }
  10. }
Of course you must to define spaceOcured at the begining of copyf function, for example to char and intialise it to zero.
Last edited by andor; Oct 2nd, 2006 at 4:51 am.
Reputation Points: 251
Solved Threads: 29
Posting Whiz in Training
andor is offline Offline
274 posts
since Jun 2005
Oct 2nd, 2006
0

Re: Reading and Writing file

Click to Expand / Collapse  Quote originally posted by andor ...
  1. while((x=getc(fp1))!=EOF){
  2. if ((unsigned char) x != ' ' || spaceOcured != 1)
  3. putc(x,fp2);
  4. if ((unsigned char) x == ' '){
  5. spaceOcured = 1;
  6. }
  7. else{
  8. spaceOcured = 0;
  9. }
  10. }
Of course you must to define spaceOcured at the begining of copyf function, for example to char and intialise it to zero.
i have an error spaceOccured undefined symbol...
Reputation Points: 16
Solved Threads: 0
Light Poster
vbcielle is offline Offline
26 posts
since Sep 2006
Oct 2nd, 2006
0

Re: Reading and Writing file

i got it i initialize spaceOccured as char
char spaceOcured=0;
Reputation Points: 16
Solved Threads: 0
Light Poster
vbcielle is offline Offline
26 posts
since Sep 2006
Oct 2nd, 2006
0

Re: Reading and Writing file

Click to Expand / Collapse  Quote originally posted by vbcielle ...
i got it i initialize spaceOccured as char
char spaceOcured=0;
it doesnt create a new file

with this code whats wrong?:

  1. #include <stdio.h>
  2. char spaceOcured;
  3. void copyf(char *fname1, char *fname2){
  4. FILE *fp1,*fp2;
  5. int x;
  6. spaceOcured=0;
  7. if (((fp1=fopen(fname1,"r"))==NULL)||((fp2=fopen(fname2,"w"))==NULL))
  8. {
  9. printf("error:unable to open the filed\n");
  10. exit(1);
  11. }
  12.  
  13. while((x=getc(fp1))!=EOF){
  14. if ((unsigned char) x != ' ' || spaceOcured != 1)
  15. putc(x,fp2);
  16. if ((unsigned char) x == ' '){
  17. spaceOcured = 1;
  18. }
  19. else{
  20. spaceOcured = 0;
  21. }
  22. }
  23. fclose(fp1);
  24. fclose(fp1);
  25. }
  26. main(){
  27.  
  28. copyf("d:\modt.txt","d:\modt2.txt");
  29. getch();
  30.  
  31. }
Reputation Points: 16
Solved Threads: 0
Light Poster
vbcielle is offline Offline
26 posts
since Sep 2006
Oct 2nd, 2006
0

Re: Reading and Writing file

Hm are you shore?
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. void copyf(char *fname1, char *fname2){
  5. FILE *fp1,*fp2;
  6. int x;
  7. char spaceOcured = 0;
  8.  
  9. if (((fp1=fopen(fname1,"r"))==NULL)||((fp2=fopen(fname2,"w"))==NULL)){
  10. printf("error:unable to open the filed\n");
  11. exit(1);
  12. }
  13. while((x=getc(fp1))!=EOF){
  14. if ((unsigned char) x != ' ' || spaceOcured != 1)
  15. putc(x,fp2);
  16. if ((unsigned char) x == ' '){
  17. spaceOcured = 1;
  18. }
  19. else{
  20. spaceOcured = 0;
  21. }
  22. }
  23.  
  24. fclose(fp1);
  25. fclose(fp1);
  26. }
  27.  
  28. int main(){
  29.  
  30. copyf("modt.txt","modt2.txt");
  31.  
  32. return 0;
  33. }
Reputation Points: 251
Solved Threads: 29
Posting Whiz in Training
andor is offline Offline
274 posts
since Jun 2005
Oct 2nd, 2006
0

Re: Reading and Writing file

Click to Expand / Collapse  Quote originally posted by andor ...
Hm are you shore?
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. void copyf(char *fname1, char *fname2){
  5. FILE *fp1,*fp2;
  6. int x;
  7. char spaceOcured = 0;
  8.  
  9. if (((fp1=fopen(fname1,"r"))==NULL)||((fp2=fopen(fname2,"w"))==NULL)){
  10. printf("error:unable to open the filed\n");
  11. exit(1);
  12. }
  13. while((x=getc(fp1))!=EOF){
  14. if ((unsigned char) x != ' ' || spaceOcured != 1)
  15. putc(x,fp2);
  16. if ((unsigned char) x == ' '){
  17. spaceOcured = 1;
  18. }
  19. else{
  20. spaceOcured = 0;
  21. }
  22. }
  23.  
  24. fclose(fp1);
  25. fclose(fp1);
  26. }
  27.  
  28. int main(){
  29.  
  30. copyf("modt.txt","modt2.txt");
  31.  
  32. return 0;
  33. }
hello modt2.txt is not created...
Reputation Points: 16
Solved Threads: 0
Light Poster
vbcielle is offline Offline
26 posts
since Sep 2006
Oct 2nd, 2006
0

Re: Reading and Writing file

Click to Expand / Collapse  Quote originally posted by vbcielle ...
hello modt2.txt is not created...
The only diffrence is the while loop and the declaration of spaceOcured. There is no sense that mod2 is not created. Create a new dir and copy the c file and the modt.txt. Try again. It should create the file modt2.txt. Do you heve this error message error:unable to open the filed? If U still have problem comment the while loop and leave only the fopen functions and see the result.
Last edited by andor; Oct 2nd, 2006 at 6:03 am.
Reputation Points: 251
Solved Threads: 29
Posting Whiz in Training
andor is offline Offline
274 posts
since Jun 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: Binary Search of an array of three strings
Next Thread in C Forum Timeline: passing strings





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


Follow us on Twitter


© 2011 DaniWeb® LLC