Monday, 15 July 2013

CSV File Input in C using Structures -



CSV File Input in C using Structures -

i want print info .csv file line line separated comma delimeter. code prints garbage value .

enum gender{ m, f }; struct student{ int stud_no; enum gender stud_gen; char stud_name[100]; int stud_marks; }; void main() { struct pupil s[60]; int i=0,j,roll_no,marks,k,select; file *input; file *output; struct pupil temp; input=fopen("internal test 1 marks mca sem 1 oct 2014 - cs 101.csv","r"); output=fopen("out.txt","a"); if (input == null) { printf("error opening file...!!!"); } while(fscanf(input,"%d,%c,%100[^,],%d", &s[i].stud_no,&s[i].stud_gen,&s[i].stud_name,&s[i].stud_marks)!=eof) { printf("\n%d,%c,%s,%d", s[i].stud_no,s[i].stud_gen,s[i].stud_name,s[i].stud_marks); i++; } }

i tried code from: read .csv file in c prints nth field. want display fields line line.

here sample input. 1401,f,fernandes suzanna ,13 1402,m,parsekar vipul vilas,14 1403,m,sequeira clayton diogo,8 1404,m,fernandes glenn ,17 1405,f,chandravarkar tanushree rohit,15

while there number of ways parse line components, 1 way can increment understanding utilize start , end pointer work downwards each line identifying commas, replacing them null-terminators (i.e. '\0' or 0), reading field, restoring comma , moving next field. manual application of strtok. next illustration can see going on. can, of course, replace utilize of start , end pointers (sp & p, respectively) strtok.

read through code , allow me know if have questions:

#include <stdio.h> #include <stdlib.h> #include <string.h> /* maximum number of pupil allocate */ #define maxs 256 enum gender { m, f }; typedef struct { /* create typedef struct */ int stud_no; enum gender stud_gen; char *stud_name; int stud_marks; } student; int main (int argc, char *argv[]) { if (argc < 2) { printf ("filename.csv please...\n"); homecoming 1; } char *line = null; /* pointer utilize getline () */ ssize_t read = 0; /* characters read getline () */ size_t n = 0; /* number of bytes allocate */ pupil **students = null; /* ptr array of stuct pupil */ char *sp = null; /* start pointer parsing line */ char *p = null; /* end pointer utilize parsing line */ int field = 0; /* counter field in line */ int cnt = 0; /* counter number allocated */ int = 0; /* simple iterator variable */ file *fp; fp = fopen (argv[1], "r"); /* open file , read */ if (!fp) { fprintf (stderr, "failed open file reading\n"); homecoming 1; } students = calloc (maxs, sizeof (*students)); /* allocate 256 ptrs set null */ while ((read = getline (&line, &n, fp)) != -1) { /* read each line in input file */ sp = p = line; /* set start ptr , ptr origin of line */ field = 0; /* set/reset field 0 */ students[cnt] = malloc (sizeof (**students)); /* alloc each stuct malloc */ while (*p) /* each character in line */ { if (*p == ',') /* if ',' end of field found */ { *p = 0; /* set null-term char (temp) */ if (field == 0) students[cnt]->stud_no = atoi (sp); if (field == 1) { if (*sp == 'm') { students[cnt]->stud_gen = 0; } else { students[cnt]->stud_gen = 1; } } if (field == 2) students[cnt]->stud_name = strdup (sp); /* strdup allocates */ *p = ','; /* replace original ',' */ sp = p + 1; /* set new start ptr start pos */ field++; /* update field count */ } p++; /* increment pointer p */ } students[cnt]->stud_marks = atoi (sp); /* read stud_marks (sp alread set begin) */ cnt++; /* increment students count */ } fclose (fp); /* close file stream */ if (line) /* free memory allocated getline */ free (line); /* iterate on students , print */ printf ("\nthe students in class are:\n\n"); while (students[it]) { printf (" %d %c %-30s %d\n", students[it]->stud_no, (students[it]->stud_gen) ? 'f' : 'm', students[it]->stud_name, students[it]->stud_marks); it++; } printf ("\n"); /* free memory allocated struct */ = 0; while (students[it]) { if (students[it]->stud_name) free (students[it]->stud_name); free (students[it]); it++; } if (students) free (students); homecoming 0; }

input:

$ cat dat/people.dat 1401,f,fernandes suzanna ,13 1402,m,parsekar vipul vilas,14 1403,m,sequeira clayton diogo,8 1404,m,fernandes glenn ,17 1405,f,chandravarkar tanushree rohit,15

output:

$./bin/stud_struct dat/people.dat students in class are: 1401 f fernandes suzanna 13 1402 m parsekar vipul vilas 14 1403 m sequeira clayton diogo 8 1404 m fernandes glenn 17 1405 f chandravarkar tanushree rohit 15

valgrind memcheck:

i have updated code insure allocated memory freed prevent against memory leaks. simple things automatic allocation of memory line getline or failing close file stream can result in little memory leaks. below valgrind memcheck confirmation.

valgrind ./bin/stud_struct dat/people.dat ==11780== memcheck, memory error detector ==11780== copyright (c) 2002-2012, , gnu gpl'd, julian seward et al. ==11780== using valgrind-3.8.1 , libvex; rerun -h copyright info ==11780== command: ./bin/stud_struct dat/people.dat ==11780== students in class are: 1401 f fernandes suzanna 13 1402 m parsekar vipul vilas 14 1403 m sequeira clayton diogo 8 1404 m fernandes glenn 17 1405 f chandravarkar tanushree rohit 15 ==11780== ==11780== heap summary: ==11780== in utilize @ exit: 0 bytes in 0 blocks ==11780== total heap usage: 13 allocs, 13 frees, 2,966 bytes allocated ==11780== ==11780== heap blocks freed -- no leaks possible ==11780== ==11780== counts of detected , suppressed errors, rerun with: -v ==11780== error summary: 0 errors 0 contexts (suppressed: 2 2)

c csv

No comments:

Post a Comment