【问题标题】:Multiline CSV file to matrix in C多行 CSV 文件到 C 中的矩阵
【发布时间】:2013-12-21 14:04:13
【问题描述】:

我正在编写程序来计算分数。

输入文件full list.txt 看起来像:

123456,11,24,,51,,12,,76,,,531,12,,,,24,56,,,,,118,,,,,,,,,,,,,,20,,,,,
123456,11,24,,51,,12,,76,,,531,12,,,,24,56,,,,,118,,,,,,,,,,,,,,20,,,,,
123456,11,24,,51,,12,,76,,,531,12,,,,24,56,,,,,118,,,,,,,,,,,,,,20,,,,,
123456,11,24,,51,,12,,76,,,531,12,,,,24,56,,,,,118,,,,,,,,,,,,,,20,,,,,
...
123456,11,24,,51,,12,,76,,,531,12,,,,24,56,,,,,118,,,,,,,,,,,,,,20,,,,,

第一个数字是ID,其余是分数。我试图将此文件放入 c[42][1000] 的矩阵中以计算总分并按升序排列它们......但我面临的问题是: 到目前为止,该程序只计算第一行.... 任何想法如何让它存储所有这些?

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

int main(void)

    {
    int lines_allocated = 1000;
    int max_line_len = 150;
    double c[42][1000];
    char **words = (char **)malloc(sizeof(char*)*lines_allocated);
    if (words==NULL)
        {
        fprintf(stderr,"Out of memory (1).\n");
        exit(1);
        }

    FILE *fp = fopen("full list.txt", "r");
    if (fp == NULL)
        {
        fprintf(stderr,"Error opening file.\n");
        exit(2);
        }

    int i;
    for (i=0;1;i++)
        {
        int j;

        if (i >= lines_allocated)
            {
            int new_size;

            new_size = lines_allocated*2;
            words = (char **)realloc(words,sizeof(char*)*new_size);
            if (words==NULL)
                {
                fprintf(stderr,"Out of memory.\n");
                exit(3);
                }
            lines_allocated = new_size;
            }
        words[i] = (char*)malloc(max_line_len);
        if (words[i]==NULL)
            {
            fprintf(stderr,"Out of memory (3).\n");
            exit(4);
            }
        if (fgets(words[i],max_line_len-1,fp)==NULL)
            break;

        for (j=strlen(words[i])-1;j>=0 && (words[i][j]=='\n' || words[i][j]=='\r');j--)
            ;
        words[i][j]='\0';
        }

    int j;
    int k=i;
    for(j = 0; j < k; j++)
    {
        printf("%s\n", words[j]);
        char *pptr = words[j];
        int l;
        words[j][strlen(words)-1]=',';
        for (l = 0; l < 41; l++)
        {
            char *ptr = strchr(pptr, ',');
            if (ptr) 
            {
                *ptr = 0;
                c[l][j] = atof(pptr);
                pptr = ptr + 1;
            }
        }
        for (l = 0; l < 41; l++)
        {
            printf("%.2lf\n", c[l][j]);
        }
        return 0;
    }


    for (;i>=0;i--)
    free(words[i]);
    free(words);
    return 0;
    }

可能是矩阵根本不存储任何值。

【问题讨论】:

  • 程序只计算第一行return 0;在循环中。还有错别字words[j][strlen(words)-1]=',';
  • 现在它不存储最后一个逗号后的值:(

标签: c csv file-io matrix type-conversion


【解决方案1】:

BLUEPIXY,感谢关注,已解决=)

是额外的return 0

但是:

words[j][strlen(words)-1]=',';

words[j][strlen(words[j])-1]=',';

不影响矩阵。我不知道为什么它没有,但它应该,不应该吗?

最终代码如下:

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

int main(void)

    {
    int lines_allocated = 1000;
    int max_line_len = 150;
    double c[42][1000];
    char **words = (char **)malloc(sizeof(char*)*lines_allocated);
    if (words==NULL)
        {
        fprintf(stderr,"Out of memory (1).\n");
        exit(1);
        }

    FILE *fp = fopen("full list.txt", "r");
    if (fp == NULL)
        {
        fprintf(stderr,"Error opening file.\n");
        exit(2);
        }

    int i;
    for (i=0;1;i++)
        {
        int j;

        if (i >= lines_allocated)
            {
            int new_size;

            new_size = lines_allocated*2;
            words = (char **)realloc(words,sizeof(char*)*new_size);
            if (words==NULL)
                {
                fprintf(stderr,"Out of memory.\n");
                exit(3);
                }
            lines_allocated = new_size;
            }
        words[i] = (char*)malloc(max_line_len);
        if (words[i]==NULL)
            {
            fprintf(stderr,"Out of memory (3).\n");
            exit(4);
            }
        if (fgets(words[i],max_line_len-1,fp)==NULL)
            break;

        for (j=strlen(words[i])-1;j>=0 && (words[i][j]=='\n' || words[i][j]=='\r');j--)
            ;
        words[i][j]='\0';
        }

    int j;
    int k=i;
    for(j = 0; j < k; j++)
    {
        printf("%s\n", words[j]);
        char *pptr = words[j];
        int l;
        words[j][strlen(words)-1]=',';
        for (l = 0; l < 42; l++)
        {
            char *ptr = strchr(pptr, ',');
            if (ptr) 
            {
                *ptr = 0;
                c[l][j] = atof(pptr);
                pptr = ptr + 1;
            }
        }    
    }

    int l;
    for (j = 0; j < k; j++)
    {
        printf("\n");
        for (l = 0; l < 42; l++)
        {
            printf("%.2lf\t", c[l][j]);
        }
    }
    for (;i>=0;i--)
    free(words[i]);
    free(words);
    return 0;
}

但是,现在的问题是最后一个逗号后的值不包含在数组中......

【讨论】:

  • words[j][strlen(words[j])-1]=','; 是最后一个逗号替换逗号。没有效果。
猜你喜欢
  • 2015-03-15
  • 1970-01-01
  • 2013-06-23
  • 2019-02-15
  • 2015-03-28
  • 2011-12-24
  • 1970-01-01
  • 2022-01-23
  • 1970-01-01
相关资源
最近更新 更多