我已经为此程序苦苦了很长时间。目的是在不使用libcs​​v的情况下读取简单的.csv文件。
经过研究和编程后,我想到了这个实现。它几乎就在那儿,但最终失败了。

我怀疑错误与str2uint64_t(ptr, &int64_converted, &error);一致,但我不知道为什么。

如果可能会有帮助,我已经从此网页中找到的实现进行了改编:
https://cboard.cprogramming.com/c-programming/47105-how-read-csv-file.html

顺便说一下,该程序可以编译为:
gcc -o q q.c && ./q file.csv

file.csv可能类似于:

0,10,20,300,905
55,18,8,253,65
0,18,265,293,98
23,18,28,6675,86
677,20,28,293,100


实现:

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


void str2uint64_t(const char *str, uint64_t *intConverted, int *error)
{
    // Converts string to uint64_t

    *intConverted = 0;
    *error = 0;

    const char *s = str;

    int sign = *s;

    char *end;
    errno = 0;
    const uint64_t sl = strtoull(str, &end, 10);

    if (end == str)
    {
        //fprintf(stderr, "%s: not a decimal number\n", str);
        *error = 1;
    }

    else if ('\0' != *end)
    {
        //fprintf(stderr, "%s: extra characters at end of input: %s\n", str, end);
        *error = 1;
    }

    else if (ERANGE == errno)
    {
        //fprintf(stderr, "%s out of range of type uint64_t\n", str);
        *error = 1;
    }

    else if (sign == '-')
    {
        //fprintf(stderr, "%s negative\n", 0);
        //errno = ERANGE;
        *error = 1;
    }

    //return sl;
    *intConverted = sl;
}


void *newMatrix(size_t rows, size_t cols)
{
    return malloc (sizeof(uint64_t[rows][cols]));
}


void importMatrix(char CSVFilePath[], size_t rows, size_t cols, uint64_t matrix[rows][cols])
{
    size_t i, j;

    uint64_t int64_converted;
    int error = 0;


    FILE *CSVfile = fopen(CSVFilePath, "r");

    if (CSVfile == NULL)
    {
        perror("Error");
        exit(EXIT_FAILURE);
    }

    char buffer[BUFSIZ], *ptr;

    for (i = 0; fgets(buffer, sizeof buffer, CSVfile); ++i)
    {
       for (j = 0, ptr = buffer; j < rows; ++j, ++ptr)
       {
            str2uint64_t(ptr, &int64_converted, &error);

            if (error == 0)
            {
                // From https://cboard.cprogramming.com/c-programming/47105-how-read-csv-file.html >> array[i][j] = (int)strtol(ptr, &ptr, 10);
                matrix[i][j] = int64_converted;
            }

            else
            {
                printf("Failed to import matrix\n");
                exit(0);
            }
         }
      }
      fclose(CSVfile);

      putchar('\n');
}


int main(int argc, char *argv[])
{
    if (argc < 2)
    {
        fprintf(stderr, "Usage: ./<program> <file.csv>\n");
        exit(EXIT_FAILURE);
    }

    size_t rows = 5;
    size_t cols = rows;

    uint64_t (*matrix)[rows] = newMatrix(rows, cols);

    importMatrix(argv[1], rows, cols, matrix[rows][cols]);

    //////////////////////////////

    return 0;
}

最佳答案

通过geany使用gcc发布的代码会导致:

gcc    -ggdb -Wall -Wextra -Wconversion -pedantic -std=gnu11  -c "untitled.c"

untitled.c: In function ‘main’:

untitled.c:114:39: warning: passing argument 4 of ‘importMatrix’ makes pointer from integer without a cast [-Wint-conversion]
     importMatrix(argv[1], rows, cols, matrix[rows][cols]);
                                       ^~~~~~

untitled.c:58:6: note: expected ‘uint64_t (*)[(sizetype)(cols)]’ but argument is of type ‘uint64_t {aka long unsigned int}’
 void importMatrix(char CSVFilePath[], size_t rows, size_t cols, uint64_t matrix[rows][cols])
      ^~~~~~~~~~~~

Compilation finished successfully


这两个警告都是严重的。结果是编译器生成了错误的代码。请修正这些警告并编辑您的问题。

该语句在函数:main()中:

importMatrix(argv[1], rows, cols, matrix[rows][cols]);


导致(第4个参数)传递内存的内容,在数组末尾经过1:matrix[][]该语句应为:

importMatrix( argv[1], rows, cols, matrix );


OT:关于此陈述:

fprintf(stderr, "Usage: ./<program> <file.csv>\n");


该程序可以任何名称运行。因此建议声明为:

fprintf(stderr, "Usage: %s <file.csv>\n", argv[0]);


然后将显示实​​际执行名称

OT:关于此陈述:

return malloc (sizeof(uint64_t[rows][cols]));


强烈建议使用:

return malloc( sizeof( uint64_t ) * rows * cols );


然后在调用:importMatrix()后在函数:newMatrix()

这个说法:

uint64_t (*matrix)[rows] = newMatrix(rows, cols);


是不正确的,因为对newMatrix()的调用未返回指针数组,因此该语句应类似于:

typedef matrixType uint64_t matrix[rows][cols];
matrixType * matrix = newMatrix(rows, cols);

关于c - 从简单的.csv文件作为uint64_t手动导入数据,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/56855767/

10-11 23:23
查看更多