c - 使用 C 将 CSV 导入数组

标签 c csv

我有一个带有 ; 分隔符的 CSV 文件。数据是双重类型,例如: 0.66985787869876;0.254886543778;475.36552366\n 0.454585787869854;0.484254886543755578;512.36552374\n

在 Cyber​​Spock 的回答后更新了代码。 我的问题似乎是 fgets 只有(用 printf 显示)第一个值而不是完整值行

#include <stdio.h>
#include <stdlib.h>
#include <math.h>



double LoadCSV(double *Tab[][100])
{

    char* token;
    double intermed;
    char* test;
    FILE *fp;
    fp = fopen("file.csv","r");
    char line[128];
    int i=0, j=0;

    while (fgets(line, sizeof(line), fp) != NULL)
    {
        for (token = strtok(line,";"); token != NULL ; token = strtok(NULL, ";"))
        {
            token=strchr(token, "\n") ;
            intermed=atof(token);
            printf("%f", intermed);
            *Tab[i][j]= intermed;
            i++;
        }
        printf("%f", *Tab[3][j]);
        j++;

    }

   //fclose(fp);
    return 0;
}

int main(){

double Tab[50][100]; //max fixed size
int k=0;
int l=0;

LoadCSV(Tab);
while (k< 4)
    {
    printf("Tab = %f", Tab[k][l]);
    k++;
    }


}

旧帖 输入 CSV 的大小每次都可以不同:一次 3 列,另一次 46 列。

然后我认为我不能像 scanf(file, "%f";"%f";"%f", varA, varB, VarC); 这样使用 scanf;因为变量是固定的一次。

虽然我尝试了这段代码,但我对名为 c 的 var 有疑问:

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

double TabInput[100]; //max fixed size

double LoadCSV()
{

   FILE *fp;
   int c;
   char cumulate[100];
   int i=0;


   fp = fopen("file.csv","r");

   if(fp == NULL)
   {
      perror("Error in opening file");
   }

   while(1)
   {

      c = fgetc(fp);
      if( feof(fp) )
      {
         break ;
      }
      if (c != ";" || c != "\n")
      {
        printf("%s",c);//*************PROBLEM C is an int
        strcat(cumulate, d);//********PROBLEM THERE

      }
      else
      {
          TabInput[i]= atof(cumulate); //str to double
          i++;
      }
        if (c == "\n")
        {
            break ;
        }

   }
   fclose(fp);

   //return(TabInput);
}

int main(){
int j=0;
LoadCSV();
while (j< 4)
    {
    printf("Tab = %f", TabInput[j]);
    j++;
    }


}

感谢您的帮助。最好的。

最佳答案

改用strtok

char line[128];
fgets( line, sizeof(line), fp );
for (char* token = strtok( line, ";"); token != NULL; token = strtok(NULL, ";"))
{
  TabInput[i++] = atof(token);
}

因此您一次读取一行,然后使用 strtok 解析该行。

编辑:

strtok 将字符串 line 拆分为多个字符串,并返回指向\0 终止字符串的指针。您唯一需要做的就是使用 atof 将字符串转换为 float / double 。在 for 循环的每次迭代中, token 将指向 line 中的新字符串。

为了将 CSV 文件拆分成行和列,您可以这样做

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAXROWS 10
#define MAXCOLS 5

int main()
{
  int rowIndex = 0;
  double rows[MAXROWS][MAXCOLS] = {{0,0}};
  char line[128];
  char* token = NULL;
  FILE* fp = fopen("myfile.csv","r");
  if (fp != NULL)
  {
    while (fgets( line, sizeof(line), fp) != NULL && rowIndex < MAXROWS)
    {
      int colIndex = 0;
      for (token = strtok( line, ";"); token != NULL && colIndex < MAXCOLS; token = strtok(NULL, ";"))
      {
        rows[rowIndex][colIndex++] = atof(token);
      }
      rowIndex++;
    }
    fclose(fp);
  }

  for (int i = 0; i < rowIndex; ++i)
  {
    for (int j = 0; j < MAXCOLS; ++j)
      printf("%10.4lf", rows[i][j]);
    putchar('\n');
  }

  return 0;
}

关于c - 使用 C 将 CSV 导入数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32884789/

相关文章:

javascript - 更新 HTML 表以包含 header JS

c# - 数据库:按国家/地区代码分类的货币?

java - 最佳实践 : Where to resample PCM and which tool?

Coverity Scan 无法使用定义的 _GNU_SOURCE 构建 <stdlib.h>

python - df.to_csv 构造输出

mysqldump 转 utf8 格式的 csv

Python - 减少大型 CSV 文件的导入和解析时间

c - 在 OS X 10.9 上的 Xcode 6 中运行基于 Makefile 的 C 项目

c++ - 为什么此代码不返回空矩阵?

C MySQL 客户端库行为