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

Here is my code. There are Two C Programs which need to follow the above format

ID: 3791374 • Letter: H

Question

Here is my code. There are Two C Programs which need to follow the above format

TexttoBinary.C

#include <stdio.h>
#include <string.h>

int main() {

FILE *fpIn, *fpOut;

fpIn = fopen("TextToBinaryInput.txt", "r");
fpOut = fopen("TextToBinaryOutput.txt", "wb");

char firstName[255], lastName[255];
unsigned int id;
float gpa;
unsigned char firstLength, lastLength;

while(!feof(fpIn)){

   fscanf(fpIn, "%s%s%u%f", firstName, lastName, &id, &gpa);
   //printf("%s %s %u %f ", firstName, lastName, id, gpa);
   firstLength = strlen(firstName);
   lastLength = strlen(lastName);
   fwrite(&firstLength, 1, 1, fpOut);
   fwrite(firstName, 1, firstLength, fpOut);
   fwrite(&lastLength, 1, 1, fpOut);
   fwrite(lastName, 1, lastLength, fpOut);
   fwrite(&id, 4, 1, fpOut);
   fwrite(&gpa, 4, 1, fpOut);
   }

fclose(fpIn);
fclose(fpOut);

}

BinarytoText.C

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
//stdtent data type to store info
struct student
{
char firstname[255],lastname[255];
unsigned int id;
float gpa;
};
//function prototypes
int longestName(struct student data[],int n);
int shortestName(struct student data[],int n);
int highestId(struct student data[],int n);
int lowestId(struct student data[],int n);
int highestGPA(struct student data[],int n);
int lowestGPA(struct student data[],int n);
////////////////////
int main()
{
struct student data[20]; // array of student
int n,i;
float tgpa;
FILE *fp,*fout;
int longestname,shortestname;
int highestid,lowestid;
int highestgpa,lowestgpa;
fp=fopen("bina.dat","rb"); // open the binary file
  
if(fp==NULL)
{
printf("Unable to open file");
exit(0);
}
fout=fopen("tex.txt","w"); //open the text file
  
n=0;
//scan the binary file continiously and store the data in text file as well as in array of student structure
while(fscanf(fp,"%s%s%d%f",data[n].firstname,data[n].lastname,&data[n].id,&tgpa)!=EOF)
{
  
data[n].gpa=tgpa;
fprintf(fout,"%s %s %d %0.2f ",data[n].firstname,data[n].lastname,data[n].id,tgpa);
n++;
}
// print the data
for(i=0;i<n;i++)
{
printf(" %s %s %d %0.2f",data[i].firstname,data[i].lastname,data[i].id,data[i].gpa);
}

fclose(fp);
longestname=longestName(data,n);
shortestname=shortestName(data,n);
highestid=highestId(data,n);
lowestid=lowestId(data,n);
highestgpa=highestGPA(data,n);
lowestgpa=lowestGPA(data,n);
//output to console
printf(" Longest name: %s %s",data[longestname].firstname,data[longestname].lastname);
printf(" Shortest name: %s %s",data[shortestname].firstname,data[shortestname].lastname);
printf(" Highest id: %d",data[highestid].id);
printf(" Lowest id: %d",data[lowestid].id);
printf(" Highest GPA: %0.2f",data[highestgpa].gpa);
printf(" Lowest GPA: %0.2f",data[lowestgpa].gpa);

return 0;
}
// returns the index of student having longest name
int longestName(struct student data[],int n)
{
int i,maxlen,index=0;
maxlen=strlen(data[0].firstname)+strlen(data[0].lastname);
for(i=1;i<n;i++)
{
if(strlen(data[i].firstname)+strlen(data[i].firstname)>maxlen)
{
maxlen=strlen(data[i].firstname)+strlen(data[i].firstname);
index=i;
}
}
return index;
}
  
// returns the index of student having shortest name
int shortestName(struct student data[],int n)
{
int i,minlen,index=0;
minlen=strlen(data[0].firstname)+strlen(data[0].lastname);
for(i=1;i<n;i++)
{
if(strlen(data[i].firstname)+strlen(data[i].firstname)<minlen)
{
minlen=strlen(data[i].firstname)+strlen(data[i].firstname);
index=i;
}
}
return index;
}

// returns the index of student having highest gpa
int highestGPA(struct student data[],int n)
{
int i,max,index=0;
max=data[0].gpa;
for(i=1;i<n;i++)
{
if(data[i].gpa>max)
{
max=data[i].gpa;
index=i;
}
}
return index;
}

// returns the index of student having lowest id
int lowestId(struct student data[],int n)
{
int i,min,index=0;
min=data[0].id;
for(i=1;i<n;i++)
{
if(data[i].id<min)
{
min=data[i].id;
index=i;
}
}
return index;
}

// returns the index of student having highest id
int highestId(struct student data[],int n)
{
int i,max,index=0;
max=data[0].id;
for(i=1;i<n;i++)
{
if(data[i].id>max)
{
max=data[i].id;
index=i;
}
}
return index;
}
  
// returns the index of student having lowest gpa
int lowestGPA(struct student data[],int n)
{
int i,min,index=0;
min=data[0].gpa;
for(i=1;i<n;i++)
{
if(data[i].gpa<min)
{
min=data[i].gpa;
index=i;
}
}
return index;
}
  
///end of program

Structural requirement: Your program must must contain at least the following files: A C source file containing only the main function A C source file containing only the function to convert a text file into a binary file A C source file containing only the function to convert a binary file into a text file A header file containing function prototypes for the conversion functions.

Explanation / Answer

Steps to Compile using gcc compiler on linux platform:

#1. gcc main.c binarytotext.c texttobinary.c [this will make a.out executable]

#2. ./a.out [we can run a.out in this way]

Note: Calling sequence of binaryToText_main() and textToBinary_main() depends on your functionality.

----------------------
File#1 [main.c]
----------------------
#include "header.h"
int main()
{
binaryToText_main();
textToBinary_main();
return 0;
}

--------------------------------
File#2 [texttobinary.c]
--------------------------------
#include "header.h"
int textToBinary_main()
{
   FILE *fpIn, *fpOut;
   fpIn = fopen("TextToBinaryInput.txt", "r");
   fpOut = fopen("TextToBinaryOutput.txt", "wb");
   char firstName[255], lastName[255];
   unsigned int id;
   float gpa;
   unsigned char firstLength, lastLength;
   while(!feof(fpIn))
   {
       fscanf(fpIn, "%s%s%u%f", firstName, lastName, &id, &gpa);
       //printf("%s %s %u %f ", firstName, lastName, id, gpa);
       firstLength = strlen(firstName);
       lastLength = strlen(lastName);
       fwrite(&firstLength, 1, 1, fpOut);
       fwrite(firstName, 1, firstLength, fpOut);
       fwrite(&lastLength, 1, 1, fpOut);
       fwrite(lastName, 1, lastLength, fpOut);
       fwrite(&id, 4, 1, fpOut);
       fwrite(&gpa, 4, 1, fpOut);
}
   fclose(fpIn);
   fclose(fpOut);
}

---------------------------------
File#3 [binarytotext.c]
---------------------------------
#include "header.h"

//function prototypes
int longestName(struct student data[],int n);
int shortestName(struct student data[],int n);
int highestId(struct student data[],int n);
int lowestId(struct student data[],int n);
int highestGPA(struct student data[],int n);
int lowestGPA(struct student data[],int n);

// returns the index of student having longest name


int binaryToText_main()
{
struct student data[20]; // array of student
int n,i;
float tgpa;
FILE *fp,*fout;
int longestname,shortestname;
int highestid,lowestid;
int highestgpa,lowestgpa;
fp=fopen("bina.dat","rb"); // open the binary file
  
if(fp==NULL)
{
   printf("Unable to open file");
   exit(0);
}
fout=fopen("tex.txt","w"); //open the text file
  
n=0;
//scan the binary file continiously and store the data in text file as well as in array of student structure
while(fscanf(fp,"%s%s%d%f",data[n].firstname,data[n].lastname,&data[n].id,&tgpa)!=EOF)
{
   data[n].gpa=tgpa;
   fprintf(fout,"%s %s %d %0.2f ",data[n].firstname,data[n].lastname,data[n].id,tgpa);
   n++;
}

// print the data
for(i=0;i<n;i++)
{
   printf(" %s %s %d %0.2f",data[i].firstname,data[i].lastname,data[i].id,data[i].gpa);
}

fclose(fp);
longestname=longestName(data,n);
shortestname=shortestName(data,n);
highestid=highestId(data,n);
lowestid=lowestId(data,n);
highestgpa=highestGPA(data,n);
lowestgpa=lowestGPA(data,n);
//output to console
printf(" Longest name: %s %s",data[longestname].firstname,data[longestname].lastname);
printf(" Shortest name: %s %s",data[shortestname].firstname,data[shortestname].lastname);
printf(" Highest id: %d",data[highestid].id);
printf(" Lowest id: %d",data[lowestid].id);
printf(" Highest GPA: %0.2f",data[highestgpa].gpa);
printf(" Lowest GPA: %0.2f",data[lowestgpa].gpa);
return 0;
}


int longestName(struct student data[],int n)
{
   int i,maxlen,index=0;
   maxlen=strlen(data[0].firstname)+strlen(data[0].lastname);
   for(i=1;i<n;i++)
{
   if(strlen(data[i].firstname)+strlen(data[i].firstname)>maxlen)
   {
       maxlen=strlen(data[i].firstname)+strlen(data[i].firstname);
       index=i;
   }
}
   return index;
}
  

// returns the index of student having shortest name
int shortestName(struct student data[],int n)
{
int i,minlen,index=0;
minlen=strlen(data[0].firstname)+strlen(data[0].lastname);
for(i=1;i<n;i++)
{
   if(strlen(data[i].firstname)+strlen(data[i].firstname)<minlen)
   {
       minlen=strlen(data[i].firstname)+strlen(data[i].firstname);
       index=i;
   }
}
return index;
}


// returns the index of student having highest gpa
int highestGPA(struct student data[],int n)
{
int i,max,index=0;
max=data[0].gpa;
for(i=1;i<n;i++)
{
   if(data[i].gpa>max)
   {
       max=data[i].gpa;
       index=i;
   }
}
return index;
}


// returns the index of student having lowest id
int lowestId(struct student data[],int n)
{
int i,min,index=0;
min=data[0].id;
for(i=1;i<n;i++)
{
   if(data[i].id<min)
   {
       min=data[i].id;
       index=i;
   }
}
return index;
}


// returns the index of student having highest id
int highestId(struct student data[],int n)
{
int i,max,index=0;
max=data[0].id;
for(i=1;i<n;i++)
{
   if(data[i].id>max)
   {
       max=data[i].id;
       index=i;
   }
}
return index;
}
  

// returns the index of student having lowest gpa
int lowestGPA(struct student data[],int n)
{
int i,min,index=0;
min=data[0].gpa;
for(i=1;i<n;i++)
{
   if(data[i].gpa<min)
   {
       min=data[i].gpa;
       index=i;
   }
}
return index;
}

-------------------------
File#4 [header.h]
-------------------------
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

struct student
{
char firstname[255],lastname[255];
unsigned int id;
float gpa;
};

int longestName(struct student data[],int n);
int shortestName(struct student data[],int n);
int highestId(struct student data[],int n);
int lowestId(struct student data[],int n);
int highestGPA(struct student data[],int n);
int lowestGPA(struct student data[],int n);

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote