【问题标题】:save csv data with different data type into an array in C将具有不同数据类型的csv数据保存到C中的数组中
【发布时间】:2018-03-03 08:10:29
【问题描述】:

大家好,我在尝试从 csv 文件读取数据并将其保存到数组时遇到问题,我已成功读取并将其保存为 char 类型数据,但我需要将数字保存为 int 数据输入,所以我可以使用数学运算,有人可以帮助我吗?

这是我的 csv 文件内容:

Nama,Gaji,Zakat,Gaji Bersih
Ali,1234567,,
Sofyan,2345678,,
Kholimi,3456789,,

我还需要将“Nama、Gaji、Zakat 和 Gaji Bersih”保存到一个数组中。

这是我的代码,它只能将内容从 csv 文件保存到 char 数组:

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

int main()
{
   char buffer[1024] ;
   char *record,*line;
   char nama[10][100];
   int i=0,j=0,c=0,gaji[10][100],zakat[10][100],bersih[10][100];
   int mat[100][100];
   FILE *fstream = fopen("Tugas03.csv","r");
   if(fstream == NULL)
   {
      printf("\n file opening failed ");
      return -1;
   }
   while((line=fgets(buffer,1024,fstream))!=NULL)
   {
     record = strtok(line,",");
     while(record != NULL){
       strcpy(nama[c], record);
       c++;
       printf("\t%s",record);
       mat[i][j++] = atoi(record) ;
       record = strtok(NULL,",");
     }
     ++i ;
   }
   return 0 ;
 }

请有人帮帮我...

这是我的新代码:

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

int main()
{
   char buffer[1024] ;
   char *record,*line;
   char nama[10][100],temp[100];
   int i=0,j=0,c=0,gaji[10][100],zakat[10][100],bersih[10][100];
   int mat[100][100];
   FILE *fstream = fopen("Tugas03.csv","r");
   if(fstream == NULL)
   {
      printf("\n file opening failed ");
      return -1;
   }
   while((line=fgets(buffer,1024,fstream))!=NULL)
   {
     record = strtok(line,",");
     while(record != NULL){
       if(c==0||1||2||3||4||8||12){
        strcpy(nama[c], record);
       }else if(c==5||9||13){
        strcpy(temp, record);
        gaji[c][100] = atoi(temp);
       }
       c++;
       //printf("\t%s",record);
       mat[i][j++] = atoi(record) ;
       record = strtok(NULL,",");
     }
     ++i ;
   }
   printf("\t%s\n", gaji[0]);
   printf("\t%s\n", nama[0]);
   printf("\t%s\n", temp);
   return 0 ;
 }

【问题讨论】:

  • 在您的情况下,第一个 atoi 失败。在每一行中,第一条记录是name。你注意到了吗?
  • 我一直在尝试使用 atoi 但它不起作用
  • c==0||1||2||3||4||8||12 有趣的是,这个条件总是成立的。 :)
  • c==0||c==1||c==2||c==3||c==4||c==8||c==12 怎么样这个?
  • @NeiosFlameStuxnet.:是的,这很好,但代码过于依赖正在读取的数据。

标签: c arrays csv


【解决方案1】:

目前还不清楚您希望如何存储数据,因此很难为您提供帮助。

但这是一个主要问题:

您需要在每个循环中将j 设置回零。

喜欢:

   while((line=fgets(buffer,1024,fstream))!=NULL)
   {
     j = 0;  // Add this line

     record = strtok(line,",");
     while(record != NULL){
       strcpy(nama[c], record);
       c++;
       printf("\t%s",record);
       printf(" (%d)", atoi(record));  // For test add this line
       mat[i][j++] = atoi(record) ;
       record = strtok(NULL,",");
     }
     ++i ;
   }

如果你在输入文件中运行上面的代码:

N,G,Z,GB
A,1,2,3
B,4,5,6
C,7,8,9

你会得到:

    N (0)   G (0)   Z (0)   GB (0)
    A (0)   1 (1)   2 (2)   3 (3)
    B (0)   4 (4)   5 (5)   6 (6)
    C (0)   7 (7)   8 (8)   9 (9)

如您所见,数字已根据需要转换为int

此代码的问题在于它试图将每个标记(即名称和数字)转换为int。你可以做到,但这可能不是你想要的。如果没有,你需要重新设计你的代码。

第 1 步是明确计划如何存储数据。

也许你想要类似的东西:

struct data {
    char nama[100];
    int Gaji;
    int Zakat;
    int Gaji_Bersih;
};

存储行(标题除外)

请注意,strtok 是这种解析的错误选择,因为它会跳过空标记。

【讨论】:

    猜你喜欢
    • 2020-02-25
    • 2017-10-06
    • 1970-01-01
    • 2014-11-28
    • 1970-01-01
    • 2019-11-29
    • 2020-01-09
    • 2018-01-30
    • 2019-07-24
    相关资源
    最近更新 更多