Here is my code. There are Two C Programs which need to follow the above format
TexttoBinary.C
#include
#include
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
#include
#include
//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\\t%s\\t%d\\t%0.2f\ \",data[n].firstname,data[n].lastname,data[n].id,tgpa);
n++;
}
// print the data
for(i=0;imaxlen)
{
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;imax)
{
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;imax)
{
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
Solution
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\"
in.
Here is my code. There are Two C Programs which need to follow the a.pdf
1. Here is my code. There are Two C Programs which need to follow the above format
TexttoBinary.C
#include
#include
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
#include
#include
//stdtent data type to store info
struct student
{
char firstname[255],lastname[255];
2. 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;
3. fprintf(fout,"%st%st%dt%0.2f ",data[n].firstname,data[n].lastname,data[n].id,tgpa);
n++;
}
// print the data
for(i=0;imaxlen)
{
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;imax)
{
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;imax)
{
max=data[i].id;
index=i;
}
4. }
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
Solution
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");
5. 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
6. 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,"%st%st%dt%0.2f ",data[n].firstname,data[n].lastname,data[n].id,tgpa);
n++;
}
// print the data
for(i=0;imaxlen)
{
maxlen=strlen(data[i].firstname)+strlen(data[i].firstname);
index=i;
}
}
return index;
}
// returns the index of student having shortest name
7. int shortestName(struct student data[],int n)
{
int i,minlen,index=0;
minlen=strlen(data[0].firstname)+strlen(data[0].lastname);
for(i=1;imax)
{
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;imax)
{
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
#include
#include
8. 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);