0

Hi,
I am trying to make a program which reads 6 integers , insert into a file numbers1.txt
then copy numbers1.txt content to numbers2.txt and finally print numbers2.

The problem I have is that when I am trying to copy the file to another the system crashes.

My code is

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

int main()
{
   int array[6];
   int i;
   char ch;
   FILE *fp1,*fp2;
   
   printf("Give 6 numbers :\n");
   for(i=0;i<6;i++)
     scanf("%d",&array[i]);
     
 
   fp1=fopen("numbers1.txt", "wb");
   
   for(i=0; i<6;i++)
      fprintf(fp1, "%d\n",array[i]);
   
   
   fp2=fopen("numbers2.txt","wb");
   

  while(!feof(fp1)) 
  {
    ch = fgetc(fp1);
        
    if(!feof(fp1)) 
        fputc(ch, fp2);
  }
   
   
   
   
   fclose(fp1);
   fclose(fp2);                  

   system("pause");
   return 0; 

}

Could you help me ?
Thanks a lot

2
Contributors
1
Reply
3
Views
6 Years
Discussion Span
Last Post by WaltP
0

read this

Why are you opening the file as binary if you are using fprintf() ?
After you finish writing your first file, you start reading from it. What's there to read when you are at the end of the file?

This topic 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.