简体   繁体   English

将制表符分隔的数据读取到C中的数组

[英]Read tab delimited data to array in C

I have an input file in text format that looks like: 我有一个文本格式的输入文件,如下所示:

G:  5   10  20  30
C:  24  49  4.0 30.0

I'd like to set each of these to an array, array, respectively. 我想将它们分别设置为一个数组,一个数组。 I saw from this answer reading input parameters from a text file with C , a way to read some of the values, but how would I get the arrays G and C? 我从这个答案中看到使用C读取文本文件中的输入参数 ,这是读取某些值的一种方法,但是我将如何获得数组G和C?

EDIT : 编辑

If I removed G:, and C: from the .txt file I could just run a for loop. 如果我从.txt文件中删除了G:和C :,则可以运行for循环。

double *conc = (double*)malloc(properConfigs*sizeof(double));
double *G = (double*)malloc(properConfigs*sizeof(double));

for (int i=0;i<properConfigs;i++)
    fscanf(inputfile,"%lf", &G[i]);
for (int i=0;i<properConfigs;i++)
    fscanf(inputfile,"%lf", &conc[i]); 

This would work, but I'd like to be able to account for someone saving the .txt file in a different order or at some point adding more rows (with different parameters). 这会起作用,但是我希望能够考虑以其他顺序保存.txt文件或在某个时候添加更多行(具有不同参数)的人。

I am not a fan of scanf , and would strongly encourage you to parse the line yourself. 我不是scanf的粉丝,因此强烈建议您自己分析这行。 If you insist on using scanf , I recommend using the sscanf variant for this so you can check the line beforehand to see which array to write. 如果您坚持使用scanf ,我建议为此使用sscanf变体,以便您可以事先检查该行以查看要写入哪个数组。 I'm not sure why you're using named arrays at all, though. 我不确定为什么要使用命名数组。 C is not very good at introspection, and you can make your program more flexible without trying to tie your input to particular symbols. C在自省方面不太擅长,您可以使程序更灵活,而不必尝试将输入绑定到特定符号。 Something like: 就像是:

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

#define properConfigs 4
void *Malloc(size_t s);
int
main(int argc, char **argv)
{
        FILE *fp = argc > 1 ? fopen(argv[1],"r") : stdin;
        double *G = Malloc( properConfigs * sizeof *G );
        double *C = Malloc( properConfigs * sizeof *G );
        int line_count = 0;
        char line[256];

        if( fp == NULL ) {
                perror(argv[1]);
                return 1;
        }
        while( line_count += 1, fgets( line, sizeof line, fp ) != NULL ) {
                double *target = NULL;
                switch(line[0]) {
                case 'G': target = G; break;
                case 'C': target = C; break;
                }
                if( target == NULL || 4 != sscanf(
                                line, "%*s%lf%lf%lf%lf",
                                target, target+1, target+2, target+3)) {
                        fprintf(stderr, "Bad input on line %d\n", line_count);
                }
        }
        for(int i=0; i < 4; i += 1 ) {
                printf ("G[%d] = %g\tC[%d] = %g\n", i, G[i], i, C[i]);
        }


        return ferror(fp);
}
void *Malloc(size_t s) {
        void *r = malloc(s);
        if(r == NULL) {
                perror("malloc");
                exit(EXIT_FAILURE);
        }
        return r;
}

Looks like your issue is atof() in c discards any white space after the first valid number. 看起来您的问题是c中的atof()丢弃了第一个有效数字之后的所有空格。 If you want to get all of the numbers you will have to split tmpstr2 and do each element separately in atof() . 如果要获取所有数字,则必须拆分tmpstr2并在atof()分别处理每个元素。

You can use strtok to split it into tokens then use atof() on each. 您可以使用strtok将其拆分为令牌,然后在每个令牌上使用atof()

char temp[];
char *nums;
nums = strtok(temp, " \t");
int count = 0;
while (nums != NULL)
{
    G[count] = atof(chrs);
    nums = strtok(NULL, " \t");
    count++;
}

Of course that is if you know before hand how many numbers you are going to get. 当然,如果您事先知道要获得多少个数字。

View this article for more info: Split string with delimiters in C 查看本文以获取更多信息: 在C中使用分隔符分割字符串

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

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