简体   繁体   English

在C中将strtof值分配给2D数组

[英]assigning strtof value into 2D array in C

I'm trying to parse a CSV file into a 2D array in C. I want to make a matrix wit the structure : 我正在尝试将CS​​V文件解析为C中的2D数组。我想使矩阵结构如下:

typedef struct {
    int row;
    int col;
    float **arr;
    int numElements;
} Matrix;

The function I'm using takes in a dynamically allocated 2D array of floats, and is returned. 我正在使用的函数采用动态分配的2D浮点数组,并返回。 I'm reading the values in using fgets, tokenizing each value between commas using strtok and then converting the string returned using strtof. 我正在使用fgets读取值,使用strtok在逗号之间标记每个值,然后使用strtof转换返回的字符串。

First, I made the 2D arrays dynamically and then pass them to the function to be filled in with values, 首先,我动态制作2D数组,然后将它们传递给要使用值填充的函数,

RMatrix->arr = make2DArray(RMatrix->row, RMatrix->col);
    VMatrix->arr = make2DArray(VMatrix->row, VMatrix->col);

    printf("RMatrix->arr : %p \n", RMatrix->arr);
    printf("VMatrix->arr : %p \n", VMatrix->arr);

    parseCSV(fpRMatrix, RMatrix->arr, RMatrix->row, RMatrix->col, &(RMatrix->numElements), INPUT_LENGTH);
    printf("RMatrix parsed\n");
    parseCSV(fpVMatrix, VMatrix->arr, VMatrix->row, VMatrix->col, &(VMatrix->numElements), INPUT_LENGTH);
    printf("VMatrix parsed\n");

Below are the functions : 以下是功能:

void parseCSV(FILE *fp, float **output, int row, int col, int *numElements ,int inputLength)
{
    char *buffer;
    int rowArr = 0;

    printf("Output : %p \n", output);

    buffer = (char*) malloc(inputLength * sizeof(char));

    while(fgets(buffer, inputLength, fp)) {
        char *p =strtok(buffer,",");
        int colArr = 0;         
        float check = 0;

        while(p)
        {
            printf("p now : %s \n", p);
            check = strtof(p, (char**) NULL);
            printf("check now : %f \n", check);
            output[rowArr][colArr] = strtof(p, (char**) NULL);
            *numElements += 1;
            colArr++;
            p = strtok('\0',",");
            printf("output[%d][%d] : %f  ", rowArr, colArr, output[rowArr][colArr]);
        }
        printf("\n");
        rowArr++;
    }
    printf("numElements in the end : %d\n", *numElements);
    free(buffer);

}

float **make2DArray(int row, int col) 
{  
    float** arr;
    float* temp;

    arr = (float**)malloc(row * sizeof(float*));
    temp = (float*)malloc(row * col * sizeof(float));
    for (int i = 0; i < row; i++) {
        arr[i] = temp + (i * row);
    }

    return arr;

}

The output : 输出 :

Name : RMatrix
NumElements : 0
Rows : 2
Cols : 4
Name : VMatrix
NumElements : 0
Rows : 2
Cols : 4
RMatrix->arr : 0x11684d0 
VMatrix->arr : 0x1168520 
Output : 0x11684d0 
p now : 1 
check now : 1.000000 
output[0][1] : 0.000000  p now : 2 
check now : 2.000000 
output[0][2] : 0.000000  p now : 3 
check now : 3.000000 
output[0][3] : 0.000000  p now : 4

check now : 4.000000 
output[0][4] : 0.000000  
p now : 5 
check now : 5.000000 
output[1][1] : 4.000000  p now : 6 
check now : 6.000000 
output[1][2] : 0.000000  p now : 7 
check now : 7.000000 
output[1][3] : 0.000000  p now : 8

check now : 8.000000 
output[1][4] : 0.000000  
numElements in the end : 8
RMatrix parsed
Output : 0x1168520 
p now : 1 
check now : 1.000000 
output[0][1] : 0.000000  p now : 2 
check now : 2.000000 
output[0][2] : 0.000000  p now : 3 
check now : 3.000000 
output[0][3] : 0.000000  p now : 4

check now : 4.000000 
output[0][4] : 0.000000  
p now : 5 
check now : 5.000000 
output[1][1] : 4.000000  p now : 6 
check now : 6.000000 
output[1][2] : 0.000000  p now : 7 
check now : 7.000000 
output[1][3] : 0.000000  p now : 8

check now : 8.000000 
output[1][4] : 0.000000  
numElements in the end : 8
VMatrix parsed

As you can see, the strtof call succeeded (reflected in p and check variable) but not the assignment into the array. 如您所见,strtof调用成功(反映在p和check变量中),但未成功分配给数组。

I've only been using C for a month and I'm fascinated by it. 我只使用C了一个月,就被它迷住了。 However, it's obvious I need to learn more. 但是,很明显,我需要学习更多。 I really appreciate your help :) 非常感谢您的帮助:)

this 这个

arr[i] = temp + (i * row);

should be 应该

arr[i] = temp + (i * col);

since i = [0,row-1] 由于i = [0,row-1]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM