I have a csv file which has 2 columns named; timestamp, SNR (int values). I have to write a function which asks first the user input; which value user wants? sample file:
timestamp ; SNR
16:15:12:468 ; 15
16:15:12:968 ; 20
For Example: If I enter SNR, the function should give me the column no. of SNR; (that is column 2 here) as well as the values of SNR.
Output : Col. no. is 2
15 /* time difference of ((16:15:12:968)-(16:15:12:458) = 500ms between these two output values*/
20
But these values should be given as output on a certain time interval. This implies that the timestamp column has to be read first and the difference between the two timestamp (current & next) values should be calculated. Now the SNR should be given as output on the interval of difference between these two timestamp values. I do not want to use array or structures because I don’t want to store values; I just require these values to pass on to other application on certain time interval.
I wrote the following code. I could get the user input and output the column no. of file, but I am not able to get the content of these columns. I used the switch case in my program, but I am not getting why this switch case is not working. I have also written a function to get the time difference between these two timestamps, but I am not getting how to combine it in this 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;
for (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;
for (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; /* To initialize the 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 be opened");
return 1;
}
buffer = malloc (BUFFER_SIZE); /*Allocate memory in buffer to read the file*/
if (buffer == NULL)
{
printf("Error: Out of Memory");
return 1;
}
fgets(line, BUFFER_SIZE, fp);
printf ("%s", line);
printf ("enter your input\n");
scanf("%s" , &text);
for (tok = strtok(line, ";");tok && *tok;tok = strtok(NULL, ";\n"))
{
value = strcmp (tok, text);
if(value ==0)
printf("col. no. is %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);
}