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

#include #include #include #include \"airPdata.h\" #define MAX_RECORDS 1000 #def

ID: 3588937 • Letter: #

Question

#include
#include
#include
#include "airPdata.h"
#define MAX_RECORDS 1000
#define BUFFER_SIZE 500
int LoadFile(FILE fptr,airPdata airport[] );
void PrintData(airPdata *airport);
int main(int argc, char** argv)
{
FILE *fptr;
airPdata* airports[MAX_RECORDS];
int count = 0;
if(argc != 2)
{
fprintf(stderr, "ERROR: need input filename as command line argument " );
exit(1);
}
fptr = fopen(argv[1], "r");
if(!fptr)
{
fprintf(stderr, "ERROR: File %s not found.", argv[1]);
exit(1);
}
count = LoadFile(fptr, airports);
fclose(fptr);
printf("%-12s %-11s %-42s %-34s %-3s %-15s %-16s Tower ",

"FAA Site", "Short Name", "Airport Name", "City", "ST", "Latitude", "Longitude");

printf("%-12s %-11s %-42s %-34s %-3s %-15s %-16s ===== ",

"========", "==========", "============", "====", "==", "========", "=========");

for(int i = 0; i < count; i++)
PrintData(airports[i]);
}
int LoadFile(FILE fptr, airPdata airport[] )
{
char buffer[BUFFER_SIZE];
char separator[2]=",";
char *token;
int count = 0;
while(fgets(buffer, BUFFER_SIZE, fptr))
{
airport[count] = malloc(sizeof(airPdata));
token = strtok(buffer, separator);
airport[count]->siteNumber = malloc(strlen(token));
strcpy(airport[count]->siteNumber, token);
token = strtok(NULL, separator);
airport[count]->LocID = malloc(strlen(token));
strcpy(airport[count]->LocID, token);
token = strtok(NULL, separator);
airport[count]->fieldName = malloc(strlen(token));
strcpy(airport[count]->fieldName, token);
token = strtok(NULL, separator);
airport[count]->city = malloc(strlen(token));
strcpy(airport[count]->city, token);
token = strtok(NULL, separator);
airport[count]->state = malloc(strlen(token));
strcpy(airport[count]->state, token);
token = strtok(NULL, separator);
airport[count]->latitude = malloc(strlen(token));
strcpy(airport[count]->latitude, token);
token = strtok(NULL, separator);
airport[count]->longitude = malloc(strlen(token));
strcpy(airport[count]->longitude, token);
token = strtok(NULL, separator);
airport[count]->controlTower = token[0];
count++;
}
return count;
}
void PrintData(airPdata *airport)
{
if(airport == NULL)
{
fprintf(stderr,"ERROR: received null for airport in PrintData");
return;
}
printf("%-12s %-11s %-42s %-34s %-3s %-15s %-16s %c ",
airport->siteNumber, airport->LocID, airport->fieldName, airport->city, airport->state, airport->latitude, airport->longitude, airport->controlTower );
}

Abstract This assignment is based on a class of problem solved in enterprise computing; extraction, transfor mation, and loading. This is often referred to as ETL. The inputs wi be data extracted from a leading aviation industry data and consulting firm, GCR. (See GCR.com for additional data.) The data is in a well known format where each data element is separated from the previous and following data elements by using a comma. It should be noted that this method of data manipulation is extremely common. The explicit order of the data fields and the desired outputs are defined in the "Specifications" 1 Objectives The objectives of this assignment are to demonstrate proficiency in file I/O, data structures, and dat formation using C language resources. Specifically, you will read in data from a text file, use that data to populate a data structure, and print that data to STDOUT by accessing the newly populated structure. 1.1 Extraction The first part of ETL is extraction. The filename of a text file will be passed to your program via the com- mand line. The data contained in that file is to be read into memory (i.e., extracted). Your program will be compiled and run on Eustis using the following commands: gcc -o etl hwletl.c /etl inputFile It is entirely possible that the input file either does not exist or is not where it is supposed to be. In such an event, your program should print an error message to STDERR that indicates which file is missing, then your program should exit safely. Use the following format for your error message (ileName should display the actual name of the missing file): etl ERROR: File "fileName" not found

Explanation / Answer

//main.c

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#include "airPdata.h"

/*define buffer length*/

#define BUF_LEN 256

/*read a line from file or until EOF*/

void readLine(char buf[], FILE *input);

/*parse buffer to struct*/

int readStruct(char buf[], airPdata* data);

/*main function*/

int main ( int argc, char *argv[] ){

FILE *input; /*input file pointer*/

char buf[BUF_LEN]; /*buffer for one line from file*/

char temp[BUF_LEN]; /*for temporary memory to contains unused string*/

int valid; /*check if line is valid*/

airPdata* data; /*airPdata struct*/

if ( argc != 2 ) /* the program should provide input file name */

{

/* print error */

printf("%s ERROR: input file should be passed via the command line.", argv[0]);

return 0;

}

/*open file for reading*/

input = fopen(argv[1], "r");

if (input == NULL){

printf("%s ERROR: iFile "%s" not found.", argv[0], argv[1]);

return 0;

}

/*create airPdata object*/

data = (airPdata*)malloc(sizeof(airPdata));

data->siteNumber = (char*)malloc(BUF_LEN * sizeof(char));

data->LocID = (char*)malloc(BUF_LEN * sizeof(char));

data->fieldName = (char*)malloc(BUF_LEN * sizeof(char));

data->city = (char*)malloc(BUF_LEN * sizeof(char));

data->state = (char*)malloc(BUF_LEN * sizeof(char));

data->latitude = (char*)malloc(BUF_LEN * sizeof(char));

data->longitude = (char*)malloc(BUF_LEN * sizeof(char));

/*print the header*/

printf("%-13s%-11s%-45s%-45s%-7s%-16s%-16s%-5s ", "FAA Site#", "Short Name", "Airport Name", "City", "ST", "Latitude", "Longitude", "Tower");

printf("%-13s%-11s%-45s%-45s%-7s%-16s%-16s%-5s ", "==========", "==========", "================", "=======", "==", "==============", "==============", "=");

/*read line by line from file*/

readLine(buf, input);

while (strlen(buf) > 0) {

valid = 0;

/*read struct from buffer*/

if (readStruct(buf, data) == 1){

/*output*/

printf("%-13s%-11s%-45s%-45s%-7s%-16s%-16s%-5c ", data->siteNumber, data->LocID, data->fieldName, data->city,

data->state, data->latitude, data->longitude, data->controlTower);

}

/*read next line from file*/

readLine(buf, input);

}/*end while*/

/*close file*/

fclose(input);

/*free resources*/

free (data->siteNumber);

free (data->LocID);

free (data->fieldName);

free (data->city);

free (data->state);

free (data->latitude);

free (data->longitude);

free (data);

return 0;

}/*end main*/

/*read a line from file or until EOF*/

void readLine(char buf[], FILE *input){

char ch = EOF;

int index = 0;

/*ignore end line character*/

ch = getc(input);

while(ch != EOF && (ch == ' ' || ch == ' '))

{

ch = getc(input);

}

if (ch != EOF){

ch = getc(input);

while(ch != EOF && ch != ' ' && ch != ' ')

{

buf[index] = ch;

index++;

ch = getc(input);

}

}

buf[index] = '';

}

/*

parse buffer to struct

return 1 if buf is valid format

return 1 if buf is invalid format

*/

int readStruct(char buf[], airPdata* data){

int i; /*loop control variable*/

int commas = 0;

int index = 0;

char field[BUF_LEN]; /*buffer to contain a field*/

int fieldIndex = 0;

/*count the commas*/

for (i = 0; i < strlen(buf); i++)

{

if (buf[i] == ',')

{

commas++;

}

}/*end for*/

if (commas == 7)

{/*8 fields*/

for (i = 0; i < strlen(buf); i++)

{

if (buf[i] == ',' || i == strlen(buf) - 1)

{

if (i == strlen(buf) - 1)

{

field[fieldIndex++] = buf[i];

}

field[fieldIndex] = '';

switch (index)

{

case 0: /*siteNumber*/

strcpy(data->siteNumber, field);

break;

case 1: /*LocID*/

strcpy(data->LocID, field);

break;

case 2: /*fieldName*/

strcpy(data->fieldName, field);

break;

case 3: /*city*/

strcpy(data->city, field);

break;

case 4: /*state*/

strcpy(data->state, field);

break;

case 5: /*latitude*/

strcpy(data->latitude, field);

break;

case 6: /*longitude*/

strcpy(data->longitude, field);

break;

case 7: /*controlTower*/

data->controlTower = field[0];

break;

default:

break;

}

fieldIndex = 0; /*for next field buffer*/

index++; /*index of next field*/

}else{

field[fieldIndex++] = buf[i];

}

}/*end for*/

return 1;

}else{ /*all fields*/

for (i = 0; i < strlen(buf); i++)

{

if (buf[i] == ',' || i == strlen(buf) - 1)

{

if (i == strlen(buf) - 1)

{

field[fieldIndex++] = buf[i];

}

field[fieldIndex] = '';

switch (index)

{

case 0: /*siteNumber*/

strcpy(data->siteNumber, field);

break;

case 1: /*LocID*/

strcpy(data->LocID, field);

break;

case 2: /*fieldName*/

strcpy(data->fieldName, field);

break;

case 3: /*city*/

strcpy(data->city, field);

break;

case 4: /*state*/

strcpy(data->state, field);

break;

case 8: /*latitude*/

strcpy(data->latitude, field);

break;

case 9: /*longitude*/

strcpy(data->longitude, field);

break;

case 14: /*controlTower*/

data->controlTower = field[0];

break;

default:

break;

}

fieldIndex = 0; /*for next field buffer*/

index++; /*index of next field*/

}else{

field[fieldIndex++] = buf[i];

}

}/*end for*/

return 1;

}

return 0;

}

===============================================================================

//airPdata.h

#ifndef AIR_PDATA_H

#define AIR_PDATA_H

/*The structure struct airPdata*/

typedef struct airPdata{

char *siteNumber; /*FAA Site Number*/

char *LocID; /*Airports "Short Name", ie MCO*/

char *fieldName; /*Airport Name*/

char *city; /*Associated City*/

char *state; /*State*/

char *latitude; /*atitude*/

char *longitude; /*Longitude*/

char controlTower; /*Control Tower (Y/N)*/

} airPdata;

#endif

===============================================================================