阅读c++中的csv文件

s8vozzvw  于 2022-12-06  发布在  其他
关注(0)|答案(1)|浏览(214)

我正在尝试读取一个csv文件,该文件包含9列,每列包含一个人的信息。我应该将数据存储在哈希表中,并创建一个函数来查找基于姓氏的数据。这是我的代码

#include<stdio.h>
#include<stdlib.h>
#include <string.h>
#include <ctype.h>
#define tablesize 27
#define entries 21

unsigned long int collisions = 0;

typedef struct {
    char id[20];
    char depid[10];
    char surname[20];
    char forename[20];
    char age[2];
    char ptype[20];
    char gender[6];
    char nation[20];
    char religion[20];
    char occupation[20];
}dict;

dict* hashTable[tablesize]= {NULL};

unsigned long int hash_function(char* s){
    unsigned long int hash = 0;
    while(*s){
        hash = hash + *s;
        s++;
    }
    return hash%tablesize;
}

void print_table(){
    for(unsigned long int i=0;i<tablesize;i++){
        if(hashTable[i]==NULL){
            printf("%d\t---\t---\n",i);
        }
        else{
            printf("%d\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\n",i,hashTable[i]->id,hashTable[i]->depid,hashTable[i]->surname,hashTable[i]->forename,hashTable[i]->age
            ,hashTable[i]->ptype,hashTable[i]->gender,hashTable[i]->nation,hashTable[i]->religion,hashTable[i]->occupation);
        }
    }
}

void insert(dict *d){
    unsigned long int ind = hash_function(d->surname);
    for(unsigned long int i=0;i<tablesize;i++){
        unsigned long int try = (ind+i)%tablesize;
        if(hashTable[try]==NULL){
            hashTable[try] = d;
            return;
        }
        else{
            collisions++;
        }
    }
}

void printvalues(unsigned long int i){
        printf("%d\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\n",i,hashTable[i]->id,hashTable[i]->depid,hashTable[i]->surname,hashTable[i]->forename,hashTable[i]->age
        ,hashTable[i]->ptype,hashTable[i]->gender,hashTable[i]->nation,hashTable[i]->religion,hashTable[i]->occupation);
}

void search(char* name){
    unsigned long int ind = hash_function(name);
    unsigned long int f=1;
    for(unsigned long int i=0;i<tablesize;i++){
        unsigned long int try = (ind+i)%tablesize;
        if(hashTable[try]!=NULL&&strcmp(hashTable[try]->surname,name)==0){
            printvalues(try);
            f=0;
        }
    }
    if(f==1)
        printf("%s not in table\n",name);
    return;
}

int main(){
    FILE *fp = fopen("truncated.csv","r");
    if(!fp){
        printf("Error");
        return 0;
    }
    char buff[1024];
    unsigned long int row = 0, column = 0;
    dict values[entries];
    unsigned long int i=0;
    while(fgets(buff,1024,fp)){
        column=0;
        row++;
        if(row==1){
            continue;
        }
        char *field = strtok(buff,",");
        while(field){
            if(column==0){
                strcpy(values[i].id,field);
            }
            if(column==1){
                strcpy(values[i].depid,field);
            }
            if(column==2){
                strcpy(values[i].surname,field);
            }
            if(column==3){
                strcpy(values[i].forename,field);
            }
            if(column==4){
                strcpy(values[i].age,field);
            }
            if(column==5){
                strcpy(values[i].ptype,field);
            }
            if(column==6){
                strcpy(values[i].gender,field);
            }
            if(column==7){
                strcpy(values[i].nation,field);
            }
            if(column==8){
                strcpy(values[i].religion,field);
            }
            if(column==9){
                strcpy(values[i].occupation,field);
            }
            field = strtok(NULL,",");
            column++;
        }
        i++;
    }
    fclose(fp);
    for(unsigned long int i=0;i<entries;i++){
        insert(&values[i]);
    }
    //printvalues(values);
    //print_table();
    while(1){
        printf("Enter term to get frequency or type 'quit' to escape:");
        char name[20];
        scanf("%s",name);
        if(strcmp(name,"quit")==0)
            return 0;
        search(name);
    }
    return 0;
}

我面临的问题是,我有两个csv文件,一个包含60000个条目,一个只包含21个条目。当我读较小的文件时,代码工作得很好。但我没有得到任何较大文件的输出。有什么想法吗?提前感谢。

myzjeezk

myzjeezk1#

在您的代码中,只有21项的空间(行#define entries 21
所以你要解析大文件中的第22行和更多,你要尝试写入一个禁止的地方,从这一点,你进入UB区域。
解决方案:使values动态化。

int count = 0;
dict *values = NULL;
 
while(fgets(buff,1024,fp)){
  ++count;
  dict * tmp = realloc(values, sizeof (dict) * count);
  if (NULL == tmp) {
      perror("realloc");
      free(values);
      exit(1);
  } else {
      values = tmp;
  }

在while循环之后:

free(values);

相关问题