Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/56.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用c语言将.csv文件中的数据读取到多维数组?_C_Arrays_Matlab_Csv - Fatal编程技术网

如何使用c语言将.csv文件中的数据读取到多维数组?

如何使用c语言将.csv文件中的数据读取到多维数组?,c,arrays,matlab,csv,C,Arrays,Matlab,Csv,在matlab中,我们可以使用以下代码: a= csvread('filename.csv'); 但使用C编程时,我使用了以下代码,但不起作用,请帮助: int main(){ int i,j,temp,m1=0,n=0; //CSV file reading int ch; FILE *fp; fp = fopen("filename.csv","r"); // read mode if( fp == NULL ) { perror("Error while opening the fi

在matlab中,我们可以使用以下代码:

a= csvread('filename.csv');
但使用C编程时,我使用了以下代码,但不起作用,请帮助:

int main(){
int i,j,temp,m1=0,n=0;
//CSV file reading
int ch;
FILE *fp;
fp = fopen("filename.csv","r"); // read mode
if( fp == NULL )
{
  perror("Error while opening the file.\n");
  exit(EXIT_FAILURE);
}
while( ( ch = fgetc(fp) ) != EOF )
  {printf("%d",ch);}
fclose(fp);
return 0;
}

mat[i][j] = ch;
int m1 = i;
int n = j;
}

请帮忙

好的,这还没有经过广泛的测试,但它应该读取具有整数值的csv文件,并将其存储在(nx m)矩阵中

#包括
#包括
#包括
int main(int argc,字符**argv){
//CSV文件读取
int rowMaxIndex,columnMaxIndex;
int**mat;
int*matc;
inti,j,idx;
字符部分[1024];
字符*令牌;
文件*fp;
fp=fopen(“filename.csv”,“r”);//读取模式
如果(fp==NULL){
perror(“打开文件时出错。\n”);
退出(退出失败);
}
//计数循环
rowMaxIndex=0;
columnMaxIndex=0;
while(fgets(第1024部分,fp)!=NULL){
令牌=NULL;
while((令牌=strtok((令牌=NULL)?部分:NULL,“,”)!=NULL){
如果(rowMaxIndex==0){//只希望在第一个循环中增加列计数
columnMaxIndex++;
}

对于(idx=0;idxA)两个问题。csv中的值总是整数吗?在读取文件之前,您知道文件中的值的数量吗?例如,是否总是说5行4列?定义“不起作用”。(乍一看,您似乎很难实现简单的循环逻辑。)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(int argc, char **argv){

    //CSV file reading
    int rowMaxIndex,columnMaxIndex;
    int **mat;
    int *matc;
    int i,j,idx;

    char part[1024];
    char *token;
    FILE *fp;
    fp = fopen("filename.csv","r"); // read mode
    if(fp == NULL){
        perror("Error while opening the file.\n");
        exit(EXIT_FAILURE);
    }

    // count loop
    rowMaxIndex = 0;
    columnMaxIndex = 0;
    while(fgets(part,1024,fp) != NULL){
        token = NULL;

        while((token = strtok((token == NULL)?part:NULL,",")) != NULL){
            if(rowMaxIndex == 0){ // only want to increment column count on first loop
                columnMaxIndex++;
            }
            for(idx = 0;idx<strlen(token);idx++){
                if(token[idx] == '\n'){ // this assumes there will be a \n (LF) at the end of the line
                    rowMaxIndex++;
                    break;
                }
            }
        }
    }

    // allocate the matrix
    matc = malloc(rowMaxIndex*columnMaxIndex*sizeof(int));
    mat = malloc(rowMaxIndex*sizeof(int*));

    for(idx = 0;idx<rowMaxIndex;idx++){
        mat[idx] = matc+idx*columnMaxIndex;
    }

    // rewind the file to the beginning
    rewind(fp);

    // read loop
    i = j = 0;
    while(fgets(part,1024,fp) != NULL){
        token = NULL;
        while((token = strtok((token == NULL)?part:NULL,",")) != NULL){
            mat[i][j] = atoi(token);
            j = (j+1)%columnMaxIndex;
        }
        i++;
    }

    fclose(fp);
    return 0;
}