逗号分隔的文本文件到结构数组

标签 c arrays struct

这是 C 语言的作业练习,让我发疯!我已经研究了将近两天,但还没有开始工作,所以非常感谢您的帮助!!

场景如下: 我有一个逗号分隔的文本文件,如下所示:

(2005 TS15),2013-Apr-07,15.2,0.0391,8.0,0.0205,20.61,20.61,1890,21,13,APO*,0.479,M
(2010 GM23),2013-Apr-13,3.9,0.0099,0.9,0.0022,13.14,13.12,61600,24.7,7,APO*,0.195,M
(2009 SQ104),2013-Apr-22,27.8,0.0714,27.8,0.0714,8.16,8.15,67300,20.9,19,APO*,0.408,S
(2012 XF55),2013-Apr-22,32.7,0.0841,32.7,0.0840,7.33,7.33,15600,22.5,7,APO*,0.446,M

文件中有 10000 条左右的记录(与近地天体相关的数据)。

我需要能够找到 10 个最大的对象/10 个最近的对象等,所以我的计划是将数据读入一个结构数组,每个结构都基于文件中的列。我已经创建了结构并创建了它的实例以从文件中填充。

我的问题是最后的一般功能......我想......

我已经尝试将完整的结构数组初始化为该结构的空白实例,但没有任何效果....

我尝试将每一行作为字符串读取,然后根据“,”限制符对其进行标记化,但我不知道如何将标记保存到结构的每个元素(这是注释掉的结束部分...)

我尝试使用 fread() 将文件读入结构数组,但那里也没有发生任何事情(那是被注释掉的结束部分......)

我的代码如下。如果有人能指出我正确的方向,我将不胜感激!! (我希望它的格式没问题。如果我没有正确解释问题,请告诉我,我会的!!!)

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <math.h>

    #define RECORDS 10972    /* number of records in the file */

    typedef struct {
        char *objectName;
        char *CADate;
        double NomDistLD;
        double NomDistAU;
        double MinDistLD;
        double MinDistAU;
        int VRel;
        int Vinf;
        int NSig;
        float HVm;
        int ref;
        char *ObjClass;
        double Diameter;
        char *Type;
    } NEO; 

    NEO textFile [RECORDS];
    NEO passing[RECORDS];


    void menu();
    void countFile (FILE*fptr);
    void general (FILE*fptr);


    int main (void)
    {
        char filename[50];
        int choice;

        FILE* fptr;

        printf("Enter name of file to open: \n");
        scanf("%s", &filename);

        fptr = fopen(filename, "r");
        if (fptr != NULL)
            printf ("File %s opened succesfully!\n\n", filename);
        else
            printf ("File %s not found!\n", filename);

        countFile(fptr);

       do
       {
     menu();
     choice= 0;

        while(choice <1 || choice >5)
        {
            printf("\nChoose an option from the above (1-5):\n");
            scanf ("%d", &choice);
        }
            switch(choice)
            {
                case 1:
                    printf("function largest\n");
                    break;
                case 2:
                    printf("function closest\n");
                    break;
                case 3:
                    printf("function largest KE\n");
                    break;
                case 4:
                    general (fptr);
                    break;
                default:
                    break;
            }
    }

    while (choice !=5); 
        printf("\n\nProgram Exiting. Goodbye!\n\n");

fclose (fptr);

return 0;
    }



    void general (FILE*fptr)
    {
int count= 0,  fileEnd= 0, i= 0, test= 0;
char **neoArray;
char hold[200];    /* string to hold tokenised strings */
char temp[10000];   /* string to hold each record as it is read in */

char *pToken;
const char *delim = ",";    /* define delimiter to tokenising each record */

passing->objectName = "";
passing->CADate = "";
passing->NomDistLD = 0.0;
passing->NomDistAU = 0.0;
passing->MinDistLD = 0.0;
passing->MinDistAU = 0.0;
passing->VRel = 0;
passing->Vinf = 0;
passing->NSig = 0;
passing->HVm = 0.0;
passing->ref = 0;
passing->ObjClass = "";
passing->Diameter = 0.0;
passing->Type = "";


for (i=0; i<RECORDS; i++)
{
    textFile[i] = passing;
}
    }


      /*  while (!feof(fptr))
{
    test = fread(&passing, sizeof(NEO), 1, fptr);

    if(test != 0)
    {
        printf("\n%s %s %f\n", passing[0].objectName, passing[0].CADate, passing[0].NomDistLD);
    }
}


/*while(!feof (fptr))
{
    for (i=0; i<RECORDS; i++)
    {

        fscanf(fptr, "%s", temp);

        pToken = strtok(temp, delim);
        {
            strcpy(textFile[i].objectName, pToken);
        }

        pToken = strtok(NULL, delim);
        while (pToken != NULL)
        {
            strcpy(textFile[i].CADate, pToken);
        }

        pToken = strtok(NULL, delim);
        while (pToken != NULL)
        {
            textFile[i].NomDistLD = atoi(pToken);
        }

        printf("\n%s %s %f\n", textFile[i].objectName, textFile[i].CADate, textFile[i].NomDistLD);
    }

    rewind (fptr);
}
    }

最佳答案

您的问题很可能是您将字符串复制到内存的随机区域,因为您从未初始化 objectNameCADate 指针。

你最好用实际的数组替换那些指针:

char objectName[128];
char CADate[128];

关于逗号分隔的文本文件到结构数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15139184/

相关文章:

arrays - 无法在 Array Extension Swift 上从自身调用函数

Javascript - 在另一个数组中插入一个数组

c - 结构中动态数组的内存损坏?

c - 使用指针进行矩阵乘法的问题

Android ndk构建无法构建32位可执行文件

python - 你能改变python中的索引吗?

string - 如何连接两个字符串并将它们存储到相同的结构键中

c - 函数中指向动态分配的结构数组的指针是否有帮助?

c - 如何从文本文件中的制表符分隔字段中正确获取数据

c++ - 这是什么控制风格?