Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Reading Binary File and Addition: Write a C program that reads a N number of int

ID: 3883864 • Letter: R

Question

Reading Binary File and Addition: Write a C program that reads a N number of integer pairs n_1 and n_2 from a binary file f and for each of the N pairs n_1 and n_2, display their summation (i.e., n_1 + n_2) followed by a newline character in the standard output. Your program should take the name of the binary file f as a command line argument. The first 4-bytes of the binary file constitutes the value N: the number of integer pairs to follow. For each of the N integer pairs, display their summation in the ASCII format in the standard output. Each element of the pairs is represented as a 4-byte integer. Please name this C file "Problem8.c" Number Addition: Write a C program that reads N number of integer pairs n_1 and n_2 from an ASCII file f_in and for each of the N pairs n_1 and n_2, store their summation (i.e., n_1 + n_2) in a binary file f_out. Your program should take the name of the binary files f_in (argument 1) and f_out (argument 2) as command line arguments. The first line of the f_in file contains the value N and N pairs of integers n_1 and n_2 follow. For each such pair of n_1 and n_2, store the value of n_1 + n_2 in the binary file f_out: We do not need to print newlines after each summation.

Explanation / Answer

Please copy and paste both problem in seperate file. My browser was not giving permission to access those file.
Problem8.c:-
#include <stdio.h>
#include<conio.h>
struct numPair
{
long int n1,n2;
};
void main()
{
int n,sum;
struct numPair num;
FILE *fptr;
clrscr();
//program for creating f.bin file
if ((fptr = fopen("C:\f.bin","wb")) == NULL){
printf("Error! opening file");
// Program exits if the file pointer returns NULL.
exit(1);
}
for(n = 1; n < 5; n++)
{
num.n1 = n;
num.n2 = n+5;
fwrite(&num, sizeof(struct numPair), 1, fptr);
}
fclose(fptr);

//reading from the f.bin file

if ((fptr = fopen("C:\f.bin","rb")) == NULL){
printf("Error! opening file");
// Program exits if the file pointer returns NULL.
exit(1);
}
for(n = 1; n < 5; n++)
{
fread(&num, sizeof(struct numPair), 1, fptr);
printf("n1 = %d ",num.n1);
printf("n2 = %d ",num.n2);
sum = num.n1+num.n2;
printf("Summation of n1 and n2 : %d ",sum);
}
fclose(fptr);
getch();
}

Problem9.c:-

#include <stdio.h>
#include <conio.h>

struct numPair
{
long int n1,n2;
};

int main()
{
int num,sum;
FILE *fptr1,*fptr2;
struct numPair num;
clrscr();
//program for creating fin.txt ASCII file
fptr1 = fopen("C:\fin.txt","w");

if(fptr1 == NULL)
{
printf("Error!");   
exit(1);   
}

for(n = 1; n < 5; n++)
{
num.n1 = n;
num.n2 = n+5;
fwrite(&num, sizeof(struct numPair), 1, fptr1);
}
fclose(fptr1);

//program for creating fout.bin file

if ((fptr2 = fopen("C:\fout.bin","wb")) == NULL){
printf("Error! opening file");
// Program exits if the file pointer returns NULL.
exit(1);
}

  

if ((fptr1 = fopen("C:\fin.txt","r")) == NULL){
printf("Error! opening file");

// Program exits if the file pointer returns NULL.
exit(1);
}

for(n = 1; n < 5; n++)
{
fread(&num, sizeof(struct numPair), 1, fptr1);
printf("n1 = %d ",num.n1);
printf("n2 = %d ",num.n2);
sum = num.n1+num.n2;
fprintf(fptr2,"%d",sum);
printf("Summation of n1 and n2 : %d ",sum);
printf("%d pair is saved in fout.bin "n);
}
fclose(fptr1);
fclose(fptr2)
getch();
return 0;
}