954,184 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Reading and Writing file

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:

#include <stdio.h>

void copyf(char *fname1, char *fname2){
 FILE *fp1,*fp2;
 int x;

 if (((fp1=fopen(fname1,"r"))==NULL)||((fp2=fopen(fname2,"w"))==NULL))
 {
  printf("error:unable to open the filed\n");
  exit(1);
 }
  while((x=getc(fp1))!=EOF)
    putc(x,fp2);
  fclose(fp1);
  fclose(fp1);
}
main(){

 copyf("modt.txt","modt2.txt");
 
}
vbcielle
Light Poster
26 posts since Sep 2006
Reputation Points: 16
Solved Threads: 0
 

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?

andor
Posting Whiz in Training
276 posts since Jun 2005
Reputation Points: 251
Solved Threads: 29
 
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"...

vbcielle
Light Poster
26 posts since Sep 2006
Reputation Points: 16
Solved Threads: 0
 
while((x=getc(fp1))!=EOF){
      if ((unsigned char) x != ' ' || spaceOcured != 1)
         putc(x,fp2);
      if ((unsigned char) x == ' '){
         spaceOcured = 1;
      }
      else{
         spaceOcured = 0;
      }
   }

Of course you must to define spaceOcured at the begining of copyf function, for example to char and intialise it to zero.

andor
Posting Whiz in Training
276 posts since Jun 2005
Reputation Points: 251
Solved Threads: 29
 
while((x=getc(fp1))!=EOF){
      if ((unsigned char) x != ' ' || spaceOcured != 1)
         putc(x,fp2);
      if ((unsigned char) x == ' '){
         spaceOcured = 1;
      }
      else{
         spaceOcured = 0;
      }
   }
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...

vbcielle
Light Poster
26 posts since Sep 2006
Reputation Points: 16
Solved Threads: 0
 

i got it i initialize spaceOccured as char
char spaceOcured=0;

vbcielle
Light Poster
26 posts since Sep 2006
Reputation Points: 16
Solved Threads: 0
 
i got it i initialize spaceOccured as char char spaceOcured=0;

it doesnt create a new file

with this code whats wrong?:

#include <stdio.h>
char spaceOcured;
void copyf(char *fname1, char *fname2){
 FILE *fp1,*fp2;
 int x;
 spaceOcured=0;
 if (((fp1=fopen(fname1,"r"))==NULL)||((fp2=fopen(fname2,"w"))==NULL))
 {
  printf("error:unable to open the filed\n");
  exit(1);
 }

  while((x=getc(fp1))!=EOF){
      if ((unsigned char) x != ' ' || spaceOcured != 1)
         putc(x,fp2);
      if ((unsigned char) x == ' '){
         spaceOcured = 1;
      }
      else{
         spaceOcured = 0;
      }
  }
   fclose(fp1);
  fclose(fp1);
}
main(){

 copyf("d:\modt.txt","d:\modt2.txt");
 getch();

}
vbcielle
Light Poster
26 posts since Sep 2006
Reputation Points: 16
Solved Threads: 0
 

Hm are you shore?

#include <stdio.h>
#include <stdlib.h>

void copyf(char *fname1, char *fname2){
   FILE *fp1,*fp2;
   int x;
   char spaceOcured = 0;

   if (((fp1=fopen(fname1,"r"))==NULL)||((fp2=fopen(fname2,"w"))==NULL)){
      printf("error:unable to open the filed\n");
      exit(1);
   }
   while((x=getc(fp1))!=EOF){
      if ((unsigned char) x != ' ' || spaceOcured != 1)
         putc(x,fp2);
      if ((unsigned char) x == ' '){
         spaceOcured = 1;
      }
      else{
         spaceOcured = 0;
      }
   }
    
   fclose(fp1);
   fclose(fp1);
}

int main(){

   copyf("modt.txt","modt2.txt");
 
 return 0;
}
andor
Posting Whiz in Training
276 posts since Jun 2005
Reputation Points: 251
Solved Threads: 29
 

Hm are you shore?

#include <stdio.h>
#include <stdlib.h>

void copyf(char *fname1, char *fname2){
   FILE *fp1,*fp2;
   int x;
   char spaceOcured = 0;

   if (((fp1=fopen(fname1,"r"))==NULL)||((fp2=fopen(fname2,"w"))==NULL)){
      printf("error:unable to open the filed\n");
      exit(1);
   }
   while((x=getc(fp1))!=EOF){
      if ((unsigned char) x != ' ' || spaceOcured != 1)
         putc(x,fp2);
      if ((unsigned char) x == ' '){
         spaceOcured = 1;
      }
      else{
         spaceOcured = 0;
      }
   }
    
   fclose(fp1);
   fclose(fp1);
}

int main(){

   copyf("modt.txt","modt2.txt");
 
 return 0;
}

hello modt2.txt is not created...:sad:

vbcielle
Light Poster
26 posts since Sep 2006
Reputation Points: 16
Solved Threads: 0
 
hello modt2.txt is not created...:sad:


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.

andor
Posting Whiz in Training
276 posts since Jun 2005
Reputation Points: 251
Solved Threads: 29
 
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.


none ....no error messgae displayed....when i run the exe... window screen is black then quits...

okie i'll try without the while loop

vbcielle
Light Poster
26 posts since Sep 2006
Reputation Points: 16
Solved Threads: 0
 

If theres no error mesages than the prog created the file. Put getchar() at the end of the program just to be shore that U did not missed the err message.

andor
Posting Whiz in Training
276 posts since Jun 2005
Reputation Points: 251
Solved Threads: 29
 
If theres no error mesages than the prog created the file. Put getchar() at the end of the program just to be shore that U did not missed the err message.



it works!!! i havent saved my code thtas why it didnt created new fle... i did exactly the code you gave me thanks!!!:cheesy:

vbcielle
Light Poster
26 posts since Sep 2006
Reputation Points: 16
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You