将CSV数据解析为结构

时间:2016-11-24 15:26:28

标签: c csv

我必须将csv文件(名称,地址,电话......)中的数据放入我的C程序结构中。不幸的是,它没有成功。我尝试使用strtok函数在每次找到&#34 ;;"时闯入令牌。 (因为我们正在处理逗号分隔文件)。

这就是我所做的:

#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#define MAX_STR_LEN 256
#define MAX_BOOKS 256


struct estrutura
{
    int id;
    char nome[40];
    char endereco[40];
    char cidade[40];
    char pais[20];
    char cep[10];
    char nasc[12];
    char telefone[14];
    char total[20];
};

struct estrutura cliente[200];

FILE *pFile;

//allocate buffer in each line

char *buf = malloc(MAX_STR_LEN);
char *tmp; 

 void abrir();
/*  Functions to be coded
int menu();
int menu2(); //manutencao de clientes
void adicionar();
void alterar();
void excluir();
void exibir();
void pesquisar(); */

main()
{
    system("cls");
    abrir();
    //menu();
}

void abrir() //open the csv file and copy it to 
{
    /* FileStream for the Library File */
    FILE *pFile;

    /* allocation of the buffer for every line in the File */
    char *buf = malloc(MAX_STR_LEN);
    char *tmp; 

    /* if the space could not be allocated, return an error */
    if (buf == NULL) {
        printf ("No memory\n");
            }

    if ( ( pFile = fopen( "control.csv", "r" ) ) == NULL ) //Reading a file
    {
        printf( "File could not be opened.\n" );
    }

    int i = 0;
    while (fgets(buf, 255, pFile) != NULL)
    {
        if ((strlen(buf)>0) && (buf[strlen (buf) - 1] == '\n')) //checa leitura
            buf[strlen (buf) - 1] = '\0';       

        tmp = strtok(buf, ";"); 
        cliente[i].id = atoi(tmp); //atoi for int

        tmp = strtok(NULL, ";"); //use strcpy for char
        strcpy(cliente[i].nome,tmp);

        tmp = strtok(NULL, ";");
        strcpy(cliente[i].endereco, tmp);

        tmp = strtok(NULL, ";");
        strcpy(cliente[i].cidade, tmp);

        tmp = strtok(NULL, ";");
        strcpy(cliente[i].pais, tmp);

        tmp = strtok(NULL, ";");
        strcpy(cliente[i].cep, tmp);

        tmp = strtok(NULL, ";");
        strcpy(cliente[i].nasc, tmp);

        tmp = strtok(NULL, ";");
        strcpy(cliente[i].telefone, tmp);

        tmp = strtok(NULL, ";");
        strcpy(cliente[i].total, tmp);

        //tempBook.ID = atoi(buf); fix below
        printf("%i, %s, %s, %s, %s, %s, %s, %s, %s \n",i, cliente[i].id , cliente[i].nome, cliente[i].endereco, cliente[i].cidade, cliente[i].pais, cliente[i].cep, cliente[i].nasc, cliente[i].telefone, cliente[i].total);

        i++;
    }
    //free(buf);
    fclose(pFile);
}

我该如何解决这个问题?我无法成功地将数据从csv中的100个客户端复制到结构中。

从现在开始谢谢你!

1 个答案:

答案 0 :(得分:1)

这里有三个主要问题:

  1. printf("%i, %s, %s, %s, ...)中的格式字符串与参数不匹配,还需要一个%iprintf("%i, %i, %s, %s, %s, ...)

  2. 在您的代码中,您从不致电abrir(),但致电menu(),但该代码不存在,因此您的代码甚至无法编译。

    < / LI>
  3. 如果您使用的是Windows(仅限于此),则需要fopen(..., "rt"))而不是fopen(..., "r"))

  4. 此外(不会导致代码中出现实际问题):

    char *buf = malloc(MAX_STR_LEN);可以由char buf[MAX_STR_LEN];替换。如果在编译时已知内存量,则动态分配内存毫无意义。在这种情况下,你当然不能打电话给free(buf)(无论如何都会被注释掉)。

    struct estrutura cliente[200];之后的声明无用之后,您可以删除它们。

    FILE *pFile;
    
    //allocate buffer in each line
    
    char *buf = (char*)malloc(MAX_STR_LEN);
    char *tmp;
    

    否则程序应该正常工作,除非您的输入文件的字段大于struct estrutura中的字段。

相关问题