从简单的.csv文件作为uint64_t手动导入数据

时间:2019-07-02 15:29:34

标签: c csv uint64

我一直在为此程序苦苦挣扎。目的是在不使用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;
}

1 个答案:

答案 0 :(得分:1)

通过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]);

将第四个参数的内存内容传递给数组末尾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);