Note that there are some explanatory texts on larger screens.

plurals
  1. POCreating a file and copying its content to other file in c language
    primarykey
    data
    text
    <p>I need a C program to copy contents of one file to another file along with the following conditions :</p> <p>1.) The file from which we read data may or may not exist. </p> <p>2.) The file to which the data is being copied may or may not exist.</p> <p>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.</p> <p>I worked out the following code,</p> <p>At present my modified code is : </p> <pre><code>#include&lt;conio.h&gt; #include&lt;stdio.h&gt; #include&lt;stdlib.h&gt; 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(); } </code></pre> <p>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?</p> <p>What should I modify to make it work ? Suggest me any code of your choice also ... Thank you !</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload