Reading and Writing file

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Sep 2006
Posts: 26
Reputation: vbcielle is an unknown quantity at this point 
Solved Threads: 0
vbcielle vbcielle is offline Offline
Light Poster

Reading and Writing file

 
0
  #1
Oct 1st, 2006
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. }
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 275
Reputation: andor has a spectacular aura about andor has a spectacular aura about andor has a spectacular aura about 
Solved Threads: 29
andor's Avatar
andor andor is offline Offline
Posting Whiz in Training

Re: Reading and Writing file

 
0
  #2
Oct 2nd, 2006
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?
If you want to win, you must not loose (Alan Ford)
Reply With Quote Quick reply to this message  
Join Date: Sep 2006
Posts: 26
Reputation: vbcielle is an unknown quantity at this point 
Solved Threads: 0
vbcielle vbcielle is offline Offline
Light Poster

Re: Reading and Writing file

 
0
  #3
Oct 2nd, 2006
Originally Posted by andor View Post
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"...
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 275
Reputation: andor has a spectacular aura about andor has a spectacular aura about andor has a spectacular aura about 
Solved Threads: 29
andor's Avatar
andor andor is offline Offline
Posting Whiz in Training

Re: Reading and Writing file

 
0
  #4
Oct 2nd, 2006
  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.
If you want to win, you must not loose (Alan Ford)
Reply With Quote Quick reply to this message  
Join Date: Sep 2006
Posts: 26
Reputation: vbcielle is an unknown quantity at this point 
Solved Threads: 0
vbcielle vbcielle is offline Offline
Light Poster

Re: Reading and Writing file

 
0
  #5
Oct 2nd, 2006
Originally Posted by andor View Post
  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...
Reply With Quote Quick reply to this message  
Join Date: Sep 2006
Posts: 26
Reputation: vbcielle is an unknown quantity at this point 
Solved Threads: 0
vbcielle vbcielle is offline Offline
Light Poster

Re: Reading and Writing file

 
0
  #6
Oct 2nd, 2006
i got it i initialize spaceOccured as char
char spaceOcured=0;
Reply With Quote Quick reply to this message  
Join Date: Sep 2006
Posts: 26
Reputation: vbcielle is an unknown quantity at this point 
Solved Threads: 0
vbcielle vbcielle is offline Offline
Light Poster

Re: Reading and Writing file

 
0
  #7
Oct 2nd, 2006
Originally Posted by vbcielle View Post
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. }
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 275
Reputation: andor has a spectacular aura about andor has a spectacular aura about andor has a spectacular aura about 
Solved Threads: 29
andor's Avatar
andor andor is offline Offline
Posting Whiz in Training

Re: Reading and Writing file

 
0
  #8
Oct 2nd, 2006
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. }
If you want to win, you must not loose (Alan Ford)
Reply With Quote Quick reply to this message  
Join Date: Sep 2006
Posts: 26
Reputation: vbcielle is an unknown quantity at this point 
Solved Threads: 0
vbcielle vbcielle is offline Offline
Light Poster

Re: Reading and Writing file

 
0
  #9
Oct 2nd, 2006
Originally Posted by andor View Post
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...
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 275
Reputation: andor has a spectacular aura about andor has a spectacular aura about andor has a spectacular aura about 
Solved Threads: 29
andor's Avatar
andor andor is offline Offline
Posting Whiz in Training

Re: Reading and Writing file

 
0
  #10
Oct 2nd, 2006
Originally Posted by vbcielle View Post
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.
If you want to win, you must not loose (Alan Ford)
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC