Quantcast
Channel: Raspberry Pi Forums
Viewing all articles
Browse latest Browse all 3873

C/C++ • Re: Two-dimensional array of character strings in C

$
0
0

As a conversation piece, here is a simple CSV parser written in C that handles arbitrary sized tables while avoiding the use of malloc.

Code:

/*  csvparser.c -- Parse an unquoted CSV file without malloc    Written 2024 by Eric Olson */#include <stdio.h>#include <stdlib.h>#include <strings.h>#include <sys/resource.h>FILE *open_file(char *file_name,char *mode){    FILE *file_handle;    file_handle=fopen(file_name,mode);    if(file_handle==NULL){        fprintf(stderr,"Error opening %s file!\n",file_name);        exit(1);    }    return(file_handle);}typedef struct { int m,n; } Rowcol;// Find number of rows and columnsRowcol getrowcol(char *csvdata){     if(!csvdata||!*csvdata){        return (Rowcol){0,0};    }    int m=0,n=0,k=0;    for(char *p=csvdata;*p;p++){        if(*p==','){            k++;        } else if(*p=='\n'){            if(n<k) n=k;            k=0;        } else if(!k){            k++; m++;        }    }    if(n<k) n=k;    return (Rowcol){m,n};}// Parse the csvdata and make strings in placevoid makedata(Rowcol d,char *data[d.m][d.n],char *csvdata){    int i=0,j=0;    bzero(data,d.m*d.n*sizeof(char *));    if(!csvdata) return;    char *p=csvdata,*q=p;    for(;;){        if(*p==','){            *p=0;            if(i<d.m&&j<d.n) data[i][j]=q;            j++; q=++p;        } else if(*p=='\n'){            *p=0;            if(i<d.m&&j<d.n) data[i][j]=q;            i++; j=0; q=++p;        } else if(!*p){            if(i<d.m&&j<d.n) data[i][j]=q;            break;        } else {            p++;        }    }}int main(int argc, char **argv){    setrlimit(RLIMIT_STACK, // Allow a big stack        &(const struct rlimit)        {RLIM_INFINITY,RLIM_INFINITY});    if(argc<2){        fprintf(stderr,"Error no config file!\n");        exit(1);    }    FILE *conf_file=open_file(argv[1],"r");    fseek(conf_file,0,SEEK_END);    long p=ftell(conf_file); // The size of the file    char csvdata[p+1]; // Stack allocated buffer    fseek(conf_file,0,SEEK_SET);    if(fread(csvdata,p,1,conf_file)==0){        fprintf(stderr,"Unable to read file %s!\n",argv[1]);        exit(1);    }    csvdata[p]=0;    Rowcol d=getrowcol(csvdata);    char *data[d.m][d.n]; // Stack allocated matrix    makedata(d,data,csvdata);    for(int i=0;i<d.m;i++){ // Test the CSV parser        for(int j=0;j<d.n;j++){            printf("%7s",data[i][j]);        }        printf("\n");    }    // Stack allocated variables disappear on return    return 0;}
I took this code that was written by ejolson which worked well for me , I then created column view of the data in the 2nd column which I did with:-

Code:

   int  lp=d.m;   char *type [lp];     for (int j=db_start; j < lp ; j++) {        type[j] = data[j][1];   }      

All looked good, then if I add another view for the 3rd column with :-

Code:

int  lp=d.m;  char *type [lp];   char *make [lp];      for (int j=db_start; j < lp ; j++) {        type[j] = data[j][1];        make[j] = data[j][2];     }      
When I print out the data in type and make the data is the same and is the data that is in 3rd column so type is wrong what am I missing ?

Statistics: Posted by Geralds — Wed Feb 21, 2024 9:06 pm



Viewing all articles
Browse latest Browse all 3873

Trending Articles