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

Need Some help with C programming: Ive got the code I just need it to follow the

ID: 3791429 • Letter: N

Question

Need Some help with C programming:
Ive got the code I just need it to follow the given structure:

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

Ans: This should be the structure of the programs:

1. Header file containing the function prototypes for conversion functions: header.h

Program:

void textToBinary(const char* inputFileName, const char* outputFileName);

void binaryToText(const char* inputFileName, const char* outputFileName);

----------------------------------------------------------------------------------------------------------------------------------------------------------------------

2. Source file containing textToBinary function: TextToBinary.c

#include <stdio.h>
#include <string.h>
void textToBinary(const char* inputFileName, const char* outputFileName)
{
   FILE *fpIn, *fpOut;

fpIn = fopen(inputFileName, "r");
fpOut = fopen(outputFileName, "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);
}

--------------------------------------------------------------------------------------------------------------------------------------------------------------------

3. Source file containing binaryToText function: BinaryToText.c

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

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

DATA* binaryToText(const char* inputFileName, const char* outputFileName)
{
   FILE *fp,*fout;
   char firstname[255],lastname[255];
   unsigned int id;   

    float gpa;
   fp=fopen(inputFileName,"rb"); // open the binary file
   if(fp==NULL)
   {
   printf("Unable to open file");
   exit(0);
   }
   fout=fopen(outputFileName,"w"); //open the text file
   //scan the binary file continiously and store the data in text file
   while((fscanf(fp,"%s%s%d%f",firstname,lastname,id,gpa)!=EOF)
   {
       fprintf(fout,"%s %s %d %0.2f ",firstname, lastname, id, gpa);
   }
   fclose(fp);
   fclose(fout);
}

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------

4. C source file containing only main function : source.c

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include "header.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;
   int longestname,shortestname;
   int highestid,lowestid;
   int highestgpa,lowestgpa;
  
   n=0;
  
   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

-----------------------------------------------------------------------------------------------------------------------------------------------------------------

To Run the above programs you should use the following commands:

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