The program has to work in command line and can it please be done in C . files n
ID: 3850088 • Letter: T
Question
The program has to work in command line and can it please be done in C .
files needed will be here https://www.dropbox.com/sh/7ewrkosr0jokx3o/AAAc-jQsgyGXSdkF3Yt6ilJia?dl=0
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: o etl hw1et, l c gCC /etl input File 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 fileName should display the actual name of the missing file): etl ERROR: File fileName not found The input file is in CSV (comma separated values) format where each line contains the data for one airport and the fields are as printed below. Note that these fields vary in size and content. Some fields may even be empty. Also note that the data for some of the fields are a melange of types. Specifically, the FAA Site Number and both latitude and longitude contain numbers, punctuation, and text. For this assignment, treat all input data as character data.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.