Most of the time calling grep from c fails. I get my error message "grep failed" when it does. The string that calls grep is always right.
Here's the code.
Some of the file is below. output is below that.
Help is appreciated.
01/02/2008,1600,199.27,200.26,192.55,194.84,38542020,0
01/03/2008,1600,195.41,197.39,192.69,194.93,30074230,0
01/04/2008,1600,191.45,193.00,178.89,180.05,51994640,0
01/07/2008,1600,181.25,183.60,170.23,177.64,74007344,0
01/08/2008,1600,180.14,182.46,170.80,171.25,54425900,0
01/09/2008,1600,171.30,179.50,168.30,179.40,64856432,0
01/10/2008,1600,177.58,181.00,175.41,178.02,52965156,0
01/11/2008,1600,176.00,177.85,170.00,172.69,44018848,0
01/14/2008,1600,177.52,179.42,175.17,178.78,39303848,0
01/15/2008,1600,177.72,179.22,164.66,169.04,83943904,0
01/16/2008,1600,165.23,169.01,156.70,159.64,79194288,0
01/17/2008,1600,161.51,164.01,158.42,160.89,63430200,0
01/18/2008,1600,162.30,165.75,159.61,161.36,61590136,0
01/22/2008,1600,148.06,159.98,146.00,155.64,86974080,0
01/23/2008,1600,136.19,140.00,126.14,139.07,120466592,0
grep 01/17/2008 /users/doug/andy/data/IV/opens/AAPL.txt
grep failed
grep 01/18/2008 /users/doug/andy/data/IV/opens/AAPL.txt
grep failed
grep 01/22/2008 /users/doug/andy/data/IV/opens/AAPL.txt
01/22/2008,1600,148.06,159.98,146.00,155.64,86974080,0
AAPL 01/22/2008 148.060000
grep 01/23/2008 /users/doug/andy/data/IV/opens/AAPL.txt
grep failed
grep 01/24/2008 /users/doug/andy/data/IV/opens/AAPL.txt
grep failed
Here's the code.
Some of the file is below. output is below that.
Help is appreciated.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
void parse( char *record, char *delim, char**tarr);
int main (int argc, const char * argv[]) {
char *symbol, newsymbol[5],date[14], *grep, **arr;
int i, j;
float price;
FILE *corncob, *voldata;
char *buf, olddate[12];
grep = (char*)calloc(150, sizeof(char));
buf = (char*)calloc(150, sizeof(char));
arr = (char **)calloc(100, sizeof(char*));
symbol = calloc(100,sizeof(char));
if ((voldata = fopen("/users/doug/andy/data/IV/IV_surface_200_1.txt", "r")) == NULL)
{
printf("couldn't open file\n");
return(0);
}
while ( (fscanf(voldata,"%s\n",symbol)) != EOF)
{
i = 0;
while (symbol[i] != ',')
{
newsymbol[i] = symbol[i];
i++;
}
newsymbol[i] = '\0';
while (symbol[++i] != ',');
i++;
j = 0;
while (symbol[i] != ',')
{
date[j] = symbol[i];
i++;
j++;
}
date[j] = '\0';
if(strcmp(olddate,date) != 0)
{
strcpy(grep, "grep ");
strcat(grep, date);
strcat(grep, " /users/doug/andy/data/IV/opens/");
strcat(grep, newsymbol);
strcat(grep, ".txt");
printf("%s\n", grep);
corncob = popen(grep, "r");
if ( (fgets(buf,150,corncob) != NULL))
{
printf("%s\n",buf);
parse(buf,",", arr);
price = atof(arr[2]);
printf("%s %s %f\n\n", newsymbol,date, atof(arr[2]));
}
else
{
printf("grep failed\n");
}
pclose(corncob);
}
strcpy(olddate, date);
}
return 0;
}
void parse( char *record, char *delim, char**tarr)
{
char *p;
int i,fld=0, element = 15;
if ( (p = (char *) calloc(element, sizeof(char))) == NULL)
printf("no memory for p");
for (i = 0; i < 100; i ++)
{
if( (tarr[i] = (char*)calloc(element, sizeof(char))) == NULL)
printf("no memory allocated for arr\n");
}
p = strtok(record,delim);
while(p)
{
strcpy(tarr[fld],p);
fld++;
p=strtok('\0',delim);
}
}
01/02/2008,1600,199.27,200.26,192.55,194.84,38542020,0
01/03/2008,1600,195.41,197.39,192.69,194.93,30074230,0
01/04/2008,1600,191.45,193.00,178.89,180.05,51994640,0
01/07/2008,1600,181.25,183.60,170.23,177.64,74007344,0
01/08/2008,1600,180.14,182.46,170.80,171.25,54425900,0
01/09/2008,1600,171.30,179.50,168.30,179.40,64856432,0
01/10/2008,1600,177.58,181.00,175.41,178.02,52965156,0
01/11/2008,1600,176.00,177.85,170.00,172.69,44018848,0
01/14/2008,1600,177.52,179.42,175.17,178.78,39303848,0
01/15/2008,1600,177.72,179.22,164.66,169.04,83943904,0
01/16/2008,1600,165.23,169.01,156.70,159.64,79194288,0
01/17/2008,1600,161.51,164.01,158.42,160.89,63430200,0
01/18/2008,1600,162.30,165.75,159.61,161.36,61590136,0
01/22/2008,1600,148.06,159.98,146.00,155.64,86974080,0
01/23/2008,1600,136.19,140.00,126.14,139.07,120466592,0
grep 01/17/2008 /users/doug/andy/data/IV/opens/AAPL.txt
grep failed
grep 01/18/2008 /users/doug/andy/data/IV/opens/AAPL.txt
grep failed
grep 01/22/2008 /users/doug/andy/data/IV/opens/AAPL.txt
01/22/2008,1600,148.06,159.98,146.00,155.64,86974080,0
AAPL 01/22/2008 148.060000
grep 01/23/2008 /users/doug/andy/data/IV/opens/AAPL.txt
grep failed
grep 01/24/2008 /users/doug/andy/data/IV/opens/AAPL.txt
grep failed