#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 );
}
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
===============================================================================
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.