将csv文件中的值分配给结构数组(C)。

时间:2018-02-08 17:11:46

标签: c

因为某些字段是空白的,所以我不能使用sscanf或strtok。解决方案似乎是使用正常工作的strsep。我的问题是数组中的所有先前结构似乎都被当前迭代的数组覆盖。

这是struct(在airPdata.h中):

typedef struct airPdata{
  char *siteNumber; // FAA Site Number
  char *LocID; // Airport's "Short Name"
  char *fieldName; // Airport Name
  char *city; // Associated City
  char *state; // State
  char *latitude; // Latitude
  char *longitude; // Longitude
  char controlTower; // Control Tower
} airPdata;

这是我的代码:

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "airPdata.h"


int main(int argc, char* argv [])
{
    FILE *fp;
    char fileName[100];
    char buf[250];
    int i = 0,j;
    struct airPdata airports[875];
    char *token;


    //request name of file with file type
    printf("What is the name of the data file (including file type)?  ");
    scanf("%s", fileName);

    //determine if file can be succesfully opened
    fp = fopen(fileName, "r");
    if(fp == NULL)
    {
        fprintf(stderr, "Can't open input file %s, Exiting safely...\n", fileName);
        exit(EXIT_FAILURE);
    }


    //printing the header lines to stdout
    printf("\n\n%-12s%-11s%-42s%-34s%-3s%-15s%-16s Tower\n","FAA Site", "Short Name", "Airport Name", "City", "ST","Latitude", "Longitude");
    printf("%-12s%-11s%-42s%-34s%-3s%-15s%-16s =====\n","========", "==========", "============", "====", "==","========", "=========");



    //reading in data to the structs
    for(i = 0; i < 875; i++)
    {
        //allocate memory for the data in current struct
        airports[i].siteNumber = malloc(51);
        airports[i].LocID = malloc(51);
        airports[i].fieldName = malloc(51);
        airports[i].city = malloc(51);
        airports[i].state = malloc(51);
        airports[i].latitude = malloc(51);
        airports[i].longitude = malloc(51);

        //buffer for current line
        if(fgets(buf, sizeof(buf), fp) == NULL) break;
        char *line = buf;
        /*sscanf(buf, "%[^,],%[^,],%[^,],%[^,],%[^,],%*[^,],%*[^,],%*[^,],%[^,],%[^,],%*[^,],%*[^,],%*[^,],%*[^,],%[^,],%*[^,],%*[^,],%*[^,],%*d",
            airports[i].siteNumber,
            airports[i].LocID,
            airports[i].fieldName,
            airports[i].city,
            airports[i].state,
            airports[i].latitude,
            airports[i].longitude,
            &airports[i].controlTower);*/

        //iterates through the 15 values in the line (could be blank) 
        //and assigns data to values that are necessary
        for(j = 1; j <= 15; j++)
        {
            //i use a switch case because I know which values I'm going to need
            switch(j)
            {
                case 1:
                airports[i].siteNumber = strsep(&line, ",");
                break;

                case 2:
                airports[i].LocID = strsep(&line, ",");
                break;

                case 3:
                airports[i].fieldName = strsep(&line, ",");
                break;

                case 4:
                airports[i].city = strsep(&line, ",");
                break;

                case 5:
                airports[i].state = strsep(&line, ",");
                break;

                case 9:
                airports[i].latitude = strsep(&line, ",");
                break;

                case 10:
                airports[i].longitude = strsep(&line, ",");
                break;

                case 15:
                token = strsep(&line, ",");
                airports[i].controlTower = token[0];
                break;

                default:
                token = strsep(&line, ",");

            }
        }
    }

    //test printing 
    printf("%s\n", airports[0].fieldName);
    printf("%s\n", airports[1].fieldName);
    printf("%s\n", airports[2].fieldName);
    printf("%s\n", airports[3].fieldName);
    printf("%s\n", airports[4].fieldName);


    fflush(stdout);

    fclose(fp);


   return 0;
}

5行预期输入:

03406.20*H,2FD7,AIR ORLANDO,ORLANDO,FL,ASO,ORL,PR,28-26-08.0210N,081-28-23.2590W,PR,,NON-NPIAS,,N,A,,0,18400
03406.36*H,2FL5,BROOKSVILLE INTL AIRWAYS- INC,ORLANDO,FL,ASO,ORL,PR,28-25-26.0000N,081-27-35.0000W,PR,,NON-NPIAS,,N,,,0,53800
03406.18*H,32FL,MEYER,ORLANDO,FL,ASO,ORL,PR,28-30-05.0120N,081-26-39.2560W,PR,,NON-NPIAS,,N,,,0,0
03406.11*H,37FA,FLORIDA HOSPITAL,ORLANDO,FL,ASO,ORL,PR,28-34-32.0020N,081-22-06.2490W,PR,,NON-NPIAS,,N,,,0,0
03406.31*H,3FD5,ARNOLD PALMER HOSPITAL,ORLANDO,FL,ASO,ORL,PR,28-31-21.0090N,081-22-49.2520W,PR,,NON-NPIAS,,N,,,0,0

我的测试printfs的预期输出(只是获取fieldName):

AIR ORLANDO
BROOKSVILLE INTL AIRWAYS- INC
MEYER
FLORIDA HOSPITAL
ARNOLD PALMER HOSPITAL

输出我得到:

ARNOLD PALMER HOSPITAL
ARNOLD PALMER HOSPITAL
ARNOLD PALMER HOSPITAL
ARNOLD PALMER HOSPITAL
ARNOLD PALMER HOSPITAL

我无法弄清楚为什么我会得到这个输出。即使您有更好的解决方案,任何帮助将不胜感激。在此先感谢:)

0 个答案:

没有答案