#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();
}
dhiliptce 0 Newbie Poster
mad93 0 Newbie Poster
Aia 1,977 Nearly a Posting Maven
dhiliptce 0 Newbie Poster
dhiliptce 0 Newbie Poster
rajenpandit -1 Newbie Poster
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.