#include<stdio.h>
#include<stdlib.h>
#include<iostream.h>
main()
{
     FILE *fp,*fp1;
     int numbers[30];
     char *buffer;
       
     int i=0,j;
//   To read the file  
     fp=fopen("PR1.txt","r");
     if(fp==NULL){
                printf("Error: can't access file.c.\n");
                 exit(1);
      }
     else        {
          printf("File opened successfully.\n");
     for(i=0;i<10;i++)
     {            
//      while(!feof(fp)) { 
      /* loop through and store the numbers into the array */
         printf("Number- %d", i);
         fscanf(fp,"%d",&numbers[i]);
        
      
//      i++;
       }   
      }
    printf("Number of numbers read: %d\n\n", i);
    printf("The numbers are:\n");

    for(j=0 ; j<i ; j++)  /* now print them out 1 by 1 */
    { 
      printf("%d\n", numbers[j]);
    }

    fclose(fp);
   
     // To write the values to another file   
     fp1= fopen("PR2.txt","w");
     
     if(fp1=NULL) {
                 printf("Error: can't access file.c.\n");
                 exit(1);
      }
     else
     {
                 printf("File opened successfully.\n") ;
      for(i=0;i<10;i++)
      {
          fprintf(fp1,"%d\n",&numbers[i]);             
//        fwrite(&numbers[i],sizeof(numbers[i]),10,fp1);
 
        
      }                      
      }                      
      fclose(fp1);     
      cin.get();      
}

Recommended Answers

All 5 Replies

if(fp1=NULL)

You're setting fp1 to NULL, it should be:

if(fp1==NULL)

Where's your question?
Furthermore, this is a forum for the C language, and you are showing us an aberration of C++.

Use proper code tags when posting source code. Read the rules or you'll be ignored.

O.K... Sorry for not being clear... I will read the rules.

Thanks for alerting me...

You are doing a bit mistake over here...........
fprintf(fp1,"%d\n",&numbers);
please don't give '&' before 'numbers'
if u still face problem then just see my program

#include<stdio.h>

void main()
{
	FILE *p,*p1,*p2;
	int arr[]={1,2,3,4,5,6,7,8,9},i;
	p=fopen("Raj.txt","w");
	for(i=0;i<9;i++)
	{
		printf("%d\n",arr[i]);
		fprintf(p,"%d\n",arr[i]);
	}
	fclose(p);
	p1=fopen("Raj.txt","r");
	for(i=0;i<9;i++)
	{
		fscanf(p1,"%d",&arr[i]);
		printf("%d\n",arr[i]);
	}
	fclose(p1);
	p2=fopen("RajCopy.txt","w");
	for(i=0;i<9;i++)
	{
		fprintf(p2,"%d\n",arr[i]);
	}
	fclose(p2);
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.