手动将简单.csv文件中的数据作为uint64导入

手动将简单.csv文件中的数据作为uint64导入,c,csv,uint64,C,Csv,Uint64,我为这个项目奋斗了很长时间。目标是在不使用libcsv的情况下读取简单的.csv文件。 在研究和编程之后,我提出了这个实现。就快到了,但最后却失败了 我怀疑错误与str2uint64_t(ptr和int64_转换和错误)一致数组[i][j]=(int)strtol(ptr,&ptr,10); 矩阵[i][j]=int64_转换; } 其他的 { printf(“导入矩阵失败\n”); 出口(0); } } } fclose(CSVfile); putchar('\n'); } int main(

我为这个项目奋斗了很长时间。目标是在不使用libcsv的情况下读取简单的.csv文件。 在研究和编程之后,我提出了这个实现。就快到了,但最后却失败了

我怀疑错误与
str2uint64_t(ptr和int64_转换和错误)一致
如果可能有帮助,我已经将此实现与我在本网页中找到的实现进行了改编:

顺便说一下,该程序可以编译并调用为:
gcc-o 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;
}
#包括
#包括
#包括
#包括
void str2uint64_t(常量字符*str,uint64_t*intConverted,int*错误)
{
//将字符串转换为uint64\u t
*intConverted=0;
*误差=0;
const char*s=str;
整数符号=*s;
字符*结束;
errno=0;
const uint64_t sl=strtoull(str和end,10);
if(end==str)
{
//fprintf(stderr,“%s:不是十进制数字\n”,str);
*误差=1;
} 
否则如果('\0'!=*结束)
{
//fprintf(stderr,“%s:输入端的额外字符:%s\n”,str,end);
*误差=1;
} 
else if(ERANGE==errno)
{
//fprintf(stderr,“%s超出uint64类型的范围”,str);
*误差=1;
} 
else if(符号=='-')
{
//fprintf(标准字符,“%s负\n”,0);
//errno=ERANGE;
*误差=1;
}
//返回sl;
*intConverted=sl;
}
void*newMatrix(大小行、大小列)
{ 
返回malloc(sizeof(uint64_t[行][cols]);
}
void importMatrix(char CSVFilePath[],大小行,大小列,uint64矩阵[行][cols])
{
尺寸i,j;
uint64_t int64_转换;
整数误差=0;
FILE*CSVfile=fopen(CSVFilePath,“r”);
if(CSVfile==NULL)
{
佩罗(“错误”);
退出(退出失败);
}   
字符缓冲区[BUFSIZ],*ptr;
对于(i=0;fgets(缓冲区,缓冲区大小,CSVfile);+i)
{
对于(j=0,ptr=buffer;j>数组[i][j]=(int)strtol(ptr,&ptr,10);
矩阵[i][j]=int64_转换;
}
其他的
{
printf(“导入矩阵失败\n”);
出口(0);
}
}
}
fclose(CSVfile);
putchar('\n');
}
int main(int argc,char*argv[])
{
如果(argc<2)
{
fprintf(标准,“用法:./\n”);
退出(退出失败);
}
行的大小=5;
大小=行;
uint64_t(*矩阵)[行]=新矩阵(行,列);
导入矩阵(argv[1],行,列,矩阵[行][cols]);
//////////////////////////////
返回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()

结果(对于第4个参数)传递内存内容,1超过数组末尾:
matrix[][]
语句应为:

importMatrix( argv[1], rows, cols, matrix );
OT:关于此声明:

fprintf(stderr, "Usage: ./<program> <file.csv>\n");
return malloc (sizeof(uint64_t[rows][cols]));
uint64_t (*matrix)[rows] = newMatrix(rows, cols);
强烈建议使用:

return malloc( sizeof( uint64_t ) * rows * cols );
然后在函数中:
importMatrix()
调用后:
newMatrix()

本声明:

fprintf(stderr, "Usage: ./<program> <file.csv>\n");
return malloc (sizeof(uint64_t[rows][cols]));
uint64_t (*matrix)[rows] = newMatrix(rows, cols);
不正确,因为对
newMatrix()
的调用未返回指针数组,因此该语句应类似于:

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

它是如何失败的?它做错了什么?它会触发该函数中的一个
if
条件,但我一直在删除注释行中的
%
,并且没有任何消息出现。我知道当
error
变量从0变为1,导致程序停止时,它必须输入其中一个条件。您在调试器中运行过它吗?这是知道发生了什么事的最简单的方法,我没有。我用Geany编程并使用命令行(这是我第一个C程序之一,从未使用过调试器)