0

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");
 
}
2
Contributors
12
Replies
13
Views
10 Years
Discussion Span
Last Post by vbcielle
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?

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?

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"...

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.

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.

i have an error spaceOccured undefined symbol...

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();

}
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;
}
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;
}

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

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.

0

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

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.

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.

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

This article has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.