简体   繁体   English

根据CSV数据构造一个C数组

[英]Construct a C array from CSV data

So, I am trying to build ac program to be able to read star catalogues and store them in arrays for further analysis. 因此,我正在尝试构建一个ac程序,以便能够读取星级目录并将其存储在数组中以进行进一步分析。

Star catalogues usually come in .csv format and have a LOT of data on them, also some slots are empty and not all of them have int, double or float type data . 星型目录通常以.csv格式出现,并且上面有很多数据,还有一些插槽为空,并且并非所有 插槽都 具有int,double或float类型数据

I want this program to be able to read "any" star catalogue (or by that matter any .csv file). 我希望该程序能够读取“任何”星级目录(或任何.csv文件)。

My first aproaches to construct such program met the issue that arrays must have their sizes declared, I decided to bypass this by creating a line and column counter functions, to be implemented by the main function. 我构造此类程序的第一个方法遇到了以下问题:必须声明数组的大小,因此我决定通过创建行和列计数器函数(由主函数实现)来绕过此问题。

int* getfield(char* line){
    FILE *fp = fopen("./hipparcos.csv", "r");
    int ch;
    int lines=0;
    do{
        ch = fgetc(fp);
        if( ch== '\n'){
            lines++;    
        }    
    }while( ch != EOF );    

    printf("number of lines in the file %d\n",lines);
    return lines;
}

which does work well when implemented in the main function like this getfield("\\n"); 在像getfield("\\n");这样的主要函数中实现时,效果很好 so I get to see how many lines it is reading in the terminal (therefore know its counting them somehow). 因此我可以看到终端中正在读取多少行(因此知道它以某种方式对其进行计数)。

What I need is to know how to store that quantity as a variable to later declare the array size and store lines in every position, and maybe after that do a line split (and separate each line into every column). 我需要知道如何将数量存储为变量,以在以后声明数组大小并在每个位置存储行,然后在行之后拆分行(并将每行分成每列)。

Any insights into how to proceed or a more efficient approach is appreciated. 任何有关如何进行或更有效的方法的见解都将受到赞赏。

You returns just int value, so change header of function to 您只返回int值,因此将函数的标头更改为

int getfield(char* line){

this should not be a pointer. 这不应该是一个指针。

Also consider possibility of not presence of '\\n' at the end of the last line of the file (in this case you will have result 1 less than the number of rows). 还要考虑文件最后一行末尾不存在'\\n'可能性(在这种情况下,结果将比行数少1)。

EDIT: 编辑:

If you just want to count number of lines as number of '\\n' characters changed function is as follows: 如果只想将行数作为'\\n'字符数进行更改,则功能如下:

int getCharCount(char chr){
    FILE *fp = fopen("./hipparcos.csv", "r");
    int ch;
    int lines = 0;
    do{
        ch = fgetc(fp);
        if( ch == chr){
            lines++;   
        }    
    }while( ch != EOF );
    fclose(fp); 
    return lines;
}

you can call this from main() eg: 您可以从main()调用它,例如:

    printf("number of lines in the file %d\n", getCharCount('\n')); // maybe +1 needed :-)

but I understand that it is a draft of your program, so consider as an option sending file name as a parameter to your function. 但我知道这是程序的草稿,因此可以考虑将文件名作为参数发送给函数。 This make your solution more flexible. 这使您的解决方案更加灵活。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM