用C语言将CSV文件存储到浮点矩阵

用C语言将CSV文件存储到浮点矩阵,c,csv,file-io,matrix,type-conversion,C,Csv,File Io,Matrix,Type Conversion,代码不会将行的最后一个逗号后的值存储到矩阵中 full list.csv类似于: 123456,57,45,,67,,56,,63,,,72,67,,,,34,56,,,,,56,,,,,,,,,,,,,,45,,,,,1523521 123457,57,45,,67,,56,,634,,,72,67,,,,34,56,,,,,56,,,,,,,,,,,,,,45,,,1234,, 123458,57,45,,67,,56,,63,,,724,67,,,,34,56,,,,,56,,,,,,,,

代码不会将行的最后一个逗号后的值存储到矩阵中

full list.csv
类似于:

123456,57,45,,67,,56,,63,,,72,67,,,,34,56,,,,,56,,,,,,,,,,,,,,45,,,,,1523521
123457,57,45,,67,,56,,634,,,72,67,,,,34,56,,,,,56,,,,,,,,,,,,,,45,,,1234,,
123458,57,45,,67,,56,,63,,,724,67,,,,34,56,,,,,56,,,,,,,,,,,,,,45,,,,1234,
123459,57,45,,67,,56,,63,,,72,647,,,,34,56,,,,,56,,,,,,,,,,,,,,45,,,,,1234
123450,57,45,,67,,56,,63,,,72,67,,,,344,56,,,,,56,,,,,,,,,,,,,,45,,,,,124
123451,57,45,,67,,56,,63,,,72,67,,,,34,564,,,,,56,,,,,,,,,,,,,,45,,,,,
123452,57,45,,67,,56,,63,,,72,67,,,,34,56,,,,,564,,,,,,,,,,,,,,45,,,,,124
123453,57,45,,67,,56,,63,,,72,67,,,,34,56,,,,,56,,,,,,,,,,,,,,454,,,,,
123454,57,45,,67,,56,,63,,,72,67,,,,34,56,,,,,56,,,,,,,,,,,,,,45,,,,,124
123455,574,45,,67,,56,,63,,,72,67,,,,34,56,,,,,56,,,,,,,,,,,,,,45,,,,,
123465,57,454,,67,,56,,63,,,72,67,,,,34,56,,,,,56,,,,,,,,,,,,,,45,,,,,
123466,57,45,,674,,56,,63,,,72,67,,,,34,56,,,,,56,,,,,,,,,,,,,,45,,,,,124
这是我的代码:

#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.csv", "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;
        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("%.2f\t", c[l][j]);
        }
    }
    for (;i>=0;i--)
    free(words[i]);
    free(words);
    return 0;
    }
#包括
#包括
#包括
内部主(空)
{
分配的整数行数=1000;
int max_line_len=150;
双c[42][1000];
char**words=(char**)malloc(sizeof(char*)*已分配的行);
if(words==NULL)
{
fprintf(stderr,“内存不足(1)。\n”);
出口(1);
}
文件*fp=fopen(“full list.csv”,“r”);
如果(fp==NULL)
{
fprintf(stderr,“打开文件时出错。\n”);
出口(2);
}
int i;
对于(i=0;1;i++)
{
int j;
如果(i>=已分配的行)
{
int新尺寸;
新大小=分配的行数*2;
words=(char**)realloc(words,sizeof(char*)*新大小);
if(words==NULL)
{
fprintf(stderr,“内存不足”。\n”);
出口(3);
}
已分配的行数=新的行数;
}
字[i]=(字符*)malloc(最大线);
如果(字[i]==NULL)
{
fprintf(stderr,“内存不足(3)。\n”);
出口(4);
}
if(fgets(字[i],最大线长度-1,fp)=NULL)
打破
对于(j=strlen(单词[i])-1;j>=0&&(单词[i][j]='\n'| |单词[i][j]='\r');j--)
字[i][j]='\0';
}
int j;
int k=i;
对于(j=0;j=0;i--)
自由(字[i]);
免费(字);
返回0;
}

我想你错过了一个简单的例子:应该是

#include <ctype.h>
...
        char *ptr = strchr(pptr, ',');
        if (ptr) 
        {
            *ptr = 0;
            c[l][j] = atof(pptr);
            pptr = ptr + 1;
        }
        else if (isdigit(*pptr)) {
            c[l][j] = atof(pptr);
        }
#包括
...
char*ptr=strchr(pptr,,);
如果(ptr)
{
*ptr=0;
c[l][j]=atof(pptr);
pptr=ptr+1;
}
否则如果(isdigit(*pptr)){
c[l][j]=atof(pptr);
}

如何使用调试器调试代码?跟踪它并找到bug。