Вопрос

I need a C program to copy contents of one file to another file along with the following conditions :

1.) The file from which we read data may or may not exist.

2.) The file to which the data is being copied may or may not exist.

If the file exists, then the data shall be directly copied and if the file doesn't exist, there should be an option to create the file ,enter data into it and the later copying the contents of the file into the other file.

I worked out the following code,

At present my modified code is :

#include<conio.h>
#include<stdio.h>
#include<stdlib.h>
void main()
{
 FILE *f1,*f2;
 char c;
 char name1[20],name2[20];
 clrscr();
 setvbuf(stdout, 0, _IONBF, 0);
 printf("\nEnter the source file name :");
 scanf("%s",name1);
 if((f1=fopen(name1,"r"))==NULL)
 {
  fclose(f1);
  f1=fopen(name1,"w");
  printf("\nThe specified file does not exist \nNew file will be created");
  printf("\nEnter the data for the new file : ");
  while((c=getchar())!=EOF)
  {
   putc(c,f1);
  }
  fclose(f1); 
 }
 f1=fopen(name1,"r");
 printf("\nEnter the destination file name :");
 scanf("%s",name2);
 f2=fopen(name2,"w+");
 while((c=getc(f1))!=EOF)
 {
  putc(c,f2);
 }
 rewind(f1);
 rewind(f2);
 printf("The data in the source file is :\n");
 while((c=getc(f1))!=EOF)
 {
  printf("%c",c);
 }
 printf("\nThe data in the destination file is :\n");
 while((c=getc(f2))!=EOF)
 {
  printf("%c",c);
 }
 fclose(f1);
 fclose(f2);
 fflush(stdin);
 getch();
}

But the program works fine only when the source file already exists . If i create a new file then no input is being taken for the destination file name and the data in the destination file file is blank. So what should i do now?

What should I modify to make it work ? Suggest me any code of your choice also ... Thank you !

Это было полезно?

Решение

Well, I see several problems in your code but the most important one is that the expression:

(c=getc(f1))!=EOF

Will always evaluated true, so you'll run infinite loops. If you read getc documentation you'll realize that it returns an int not a char:

getc documentation

So basically what you're doing is truncating the EOF, which is an int generally defined as -1, to a char when:

c=getc(f1)  // At this point C = 255(0xFF) if getc returned EOF

And then promoting c to an int when is compared to EOF, since an int is big enough to hold 255 the comparison made is 255 != -1, which is always true.

To fix that just declare c as an int.

Some more tips:

You may also want to make sure that it was an end of file condition by using feof since getc returns EOF on other error conditions as well.

You may want to move the "\n" to the end of your senteces on printf calls to force a flush on stdout. Or alternatively you can add this at the start of your program:

setvbuf(stdout, 0, _IONBF, 0);

It seems you're storing the file names in name1 and name2, so you may want to remove the double quotes from fopen file names so that it actually use them.

When you open name2 you use write access only but at the end you try to read and display its content, you may want to use "w+" as the access mode.

When you're trying to print the resulting data the file handler in f2 has already been closed. And the file cursors are already at the end of the file so you may want to use rewind, e.g.

//fclose(f2);
rewind(f1);
rewind(f2);
printf("The data in the source file is :\n");
 while((c=getc(f1))!=EOF)
 {
  printf("%c",c);
 }
 printf("The data in the destination file is :\n");
 while((c=getc(f2))!=EOF)
 {
  printf("%c",c);
 }
 fclose(f1);
 fclose(f2);

Другие советы

finally the successful code is :

#include<conio.h>
#include<stdio.h>
#include<stdlib.h>
void main()
{
 FILE *f1,*f2;
 char c;
 char name1[20],name2[20];
 clrscr();
 printf("\nEnter the source file name :");
 scanf("%s",name1);
 if((f1=fopen(name1,"r"))==NULL)
 {
  fclose(f1);
  f1=fopen(name1,"w");
  printf("\nThe specified file does not exist \nNew file will be created");
  printf("\nEnter the data for the new file : ");
  while((c=getchar())!='^')
  {
   putc(c,f1);
  }
  fclose(f1); 
 }
 f1=fopen(name1,"r");
 printf("\nEnter the destination file name :");
 scanf("%s",name2);
 f2=fopen(name2,"w+");
 while((c=getc(f1))!=EOF)
 {
  putc(c,f2);
 }
 rewind(f1);
 rewind(f2);
 printf("The data in the source file is :\n");
 while((c=getc(f1))!=EOF)
 {
  printf("%c",c);
 }
 printf("\nThe data in the destination file is :\n");
 while((c=getc(f2))!=EOF)
 {
  printf("%c",c);
 }
 fclose(f1);
 fclose(f2);
 fflush(stdin);
 getch();
} 
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top