Reading the different columns of csv file in C -


i have csv file has 2 columns named; timestamp, snr (int values). have write function asks first user input; value user wants? sample file:

 timestamp        ;       snr     16:15:12:468     ;       15  16:15:12:968     ;       20 

for example: if enter snr, function should give me column no. of snr; (that column 2 here) values of snr.

output : col. no. 2                      15       /* time difference of ((16:15:12:968)-(16:15:12:458) = 500ms between these 2 output values*/               20 

but these values should given output on time interval. implies timestamp column has read first , difference between 2 timestamp (current & next) values should calculated. snr should given output on interval of difference between these 2 timestamp values. not want use array or structures because don’t want store values; require these values pass on other application on time interval.

i wrote following code. user input , output column no. of file, not able content of these columns. used switch case in program, not getting why switch case not working. have written function time difference between these 2 timestamps, not getting how combine in function.

#include <stdio.h> #include <stdlib.h> #include <string.h> #include <windows.h> #include <time.h> #define buffer_size 1024 #define num_rows 100  const char* gettime(char* line, int num ) {     const char* tok;     (tok = strtok(line, ";");tok && *tok;tok = strtok(null, ";\n"))     {         if (!num--)         //sleep(500);         return tok;     }     return null; } const char* getsnr(char* line, int num ) {     const char* tok;     (tok = strtok(line, ";");tok && *tok;tok = strtok(null, ";\n"))     {         if (!num--)         //sleep(500);         return atoi(tok);     }     return null; }  struct tm mytime;   int main () {     int hh, mm;     float ss, ms;     mytime.tm_year = 2015 - 1900;               /* initialize struct tm*/     mytime.tm_mon = 6;     mytime.tm_mday = 15;     int count;     int value;     char text[25];     char *buffer;     file *fp;     char *token;     char *tok;     char line [100];     char time_buffer[100];     char **timestamp;                               /*dynamic allocated array*/     unsigned long ul_second_prev ;     unsigned long ul_second_current;     int i=0, j=0, k=1;     int ui_time_diff, ui_snr;     time_t time_prev, time_current;                 /*dynamic allocated array*/     int timediff ;     timestamp = malloc(num_rows*sizeof(char*));   if ((timestamp)== null)     {         printf("error: out of memory");     }  if ((fp=fopen("testfile.csv", "r"))==null) {     printf ("file cannot opened");     return 1; }   buffer = malloc (buffer_size); /*allocate memory in buffer read file*/  if (buffer == null) {     printf("error: out of memory");     return 1; }       fgets(line, buffer_size, fp);     printf ("%s", line);     printf ("enter input\n");     scanf("%s" , &text); (tok = strtok(line, ";");tok && *tok;tok = strtok(null, ";\n")) {         value = strcmp (tok, text);         if(value ==0)             printf("col. no. %d", k);         else k++ ; }  while (fgets(line, buffer_size, fp)) {      char* tmp = strdup(line);  switch (k) {     case 1:         gettime(tmp, 1);         printf ( "%s",tok );         break;     case 2:         getsnr(tmp, 2);         printf ( "%s",tok );         break; } free(tmp); } 

ok apart myriads of unused variables , @ least 1 unused include (<windows.h>), gcc gives these warnings point out problems:

> gcc -std=c99 -wall -wextra -pedantic -oq q.c q.c: in function ‘getsnr’: q.c:26:9: warning: return makes pointer integer without cast          return atoi(tok);          ^ q.c: in function ‘main’: q.c:84:5: warning: format ‘%s’ expects argument of type ‘char *’, argument 2 has type ‘char (*)[25]’ [-wformat=]      scanf("%s" , &text);      ^ q.c:96:5: warning: implicit declaration of function ‘strdup’ [-wimplicit-function-declaration]      char* tmp = strdup(line);      ^ q.c:96:17: warning: initialization makes pointer integer without cast      char* tmp = strdup(line);                  ^ q.c:110:1: error: expected declaration or statement @ end of input  }  ^ 

your getsnr() returns integer, declared return character pointer (aka: string). discard return value , feed (uninitialized!) char *tok (which should const take return value) %s format specifier.

in scanf, apart using wrong pointer type (that's warning about), have buffer overflow. welcome crackers. never ever use scanf("%s", ...), there's no way tell how data read. use fgets(text, 25, stdin) in case here.

you should learn 2 things when programming c: first, accurate , precise, not erratic. second, let compiler warn everything (using appropriate switches if necessary). compiler warning means in 98% of cases programming error or bug.

that being said, it's silly have 2 identical functions. this:

const char* getfield(char* line, int num ) {     const char* tok;     (tok = strtok(line, ";");tok && *tok;tok = strtok(null, ";\n"))     {         if (!num--)         return tok;     }     return null; } 

still bit strange loop, that's question of style ... , later (note assign value returned function):

while (fgets(line, buffer_size, fp)) {     switch (k)     {         case 1:             tok = getfield(line, 1);             printf ( "%s",tok );             break;         case 2:             tok = getfield(line, 2);             printf ( "%d",atoi(tok) );             break;     } } 

also note allocated tmp used doesn't make sense, line buffer gets overwritten in next iteration of loop anyways. why take copy?


Comments

Popular posts from this blog

yii2 - Yii 2 Running a Cron in the basic template -

asp.net - 'System.Web.HttpContext' does not contain a definition for 'GetOwinContext' Mystery -

mercurial graft feature, can it copy? -