C program help In this program you will be converting a text
C program help
In this program, you will be converting a text file into a binary file and vice versa. You will also be carrying out other tasks on binary files The files contain information about students at an imaginary university. Each student has a first name, a last name, an id number. and a GPA Each line of a text file contains information about a single student, and will be in the following format firstname lastname id gpa Where the first name and last me are strings of at most 255 characters, t e id is a 4-byte integer, and the gpa is a 4-byt floating point number. The float will have a precision of one decimal place (e.g. 3.5). The four items are separated by single whitespace characters A binary file will contain one record for each line in its corresponding text file. The size of a record is E1 E 10 bytes, where E1 is the length of the first name, and is the name of the last name. The 10 remaining bytes consist of 1 byte to store E1, 1 byte to store l2, 4 bytes to store the id number, and 4 bytes to store the gpa. The format of a record is as follows gpa firstname En last name id 1 byte lu bytes 1 byte bytes 4 bytes A bytesSolution
Here is the code for the first problem, i.e., TextToBinary.c conversion:
#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);
}
