- #1
leon1127
- 486
- 0
Hi Guys,
I have be trying to read CSV formatted data with C/C++ with no help...
The format is following
8/29/2008,19.54,19.6,19.28,19.38,11204900,19.38
8/28/2008,19.48,19.76,19.38,19.65,11729500,19.65
8/27/2008,19.08,19.45,18.93,19.37,9300100,19.37
8/26/2008,19.12,19.2,19,19.09,8770500,19.09
8/25/2008,19.34,19.4,19.05,19.09,13779300,19.09
8/22/2008,19.11,19.68,19.1,19.53,11087500,19.53
8/21/2008,19.06,19.18,18.87,19.11,16995100,19.11
8/20/2008,19.57,19.65,19.1,19.17,16336900,19.17
8/19/2008,19.78,19.91,19.41,19.42,12837300,19.42
and my code is
// csv_read.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <cstring>
#include<stdio.h>
#include<stdlib.h>
#include <vector>
#include <iostream>
using namespace std;
struct yahoo_data
{
char date[9];
double open;
double high;
double low;
double close;
float volume;
double adj_close;
};
vector<yahoo_data> yhoo;
typedef struct yahoo_data data_format;
int _tmain(int argc, _TCHAR* argv[])
{
FILE *fp;
fp=fopen("table.csv","r");
char bufferCustNum[9];
char bufferCost[40];
int i = 0;
data_format temp;
cout<< "before while"<<endl;
while( fgets(bufferCustNum,sizeof(bufferCustNum),fp) != NULL)
{
cout<< "i = "<< i << endl;
if(i>0){
strcpy(temp.date, strtok(bufferCustNum,","));
cout<< temp.date<<endl;
/*
strcpy(temp.open, (double)strtok(NULL,","));
strcpy(temp.high, strtok(NULL,","));
strcpy(temp.low, strtok(NULL,","));
strcpy(temp.close, strtok(NULL,","));
strcpy(temp.volume, strtok(NULL,","));
strcpy(temp.adj_close, strtok(NULL,","));
*/
temp.open = (double)atof(strtok(NULL,","));
cout<< "after open"<<endl;
temp.high = atof(strtok(NULL,","));
cout<< "after high"<<endl;
temp.low = atof(strtok(NULL,","));
cout<< "after low"<<endl;
temp.close = atof(strtok(NULL,","));
cout<< "after close"<<" "<<temp.close<<endl;
// cout<< atof(strtok(NULL,","))<<endl;
temp.volume = (int)atof(strtok(NULL,","));
cout<< "after adjust close"<<endl;
temp.adj_close = atof(strtok(NULL,","));
cout<< "before push"<<endl;
yhoo.push_back(temp);
cout<< "before push"<<endl;
}
++i;
}
for (int j=0; j<10; j++)
{
}
return 0;
}
There is some unnecessary part in the code but it should compile under g++. I get some memory leaks... Anyone have idea?
I have be trying to read CSV formatted data with C/C++ with no help...
The format is following
8/29/2008,19.54,19.6,19.28,19.38,11204900,19.38
8/28/2008,19.48,19.76,19.38,19.65,11729500,19.65
8/27/2008,19.08,19.45,18.93,19.37,9300100,19.37
8/26/2008,19.12,19.2,19,19.09,8770500,19.09
8/25/2008,19.34,19.4,19.05,19.09,13779300,19.09
8/22/2008,19.11,19.68,19.1,19.53,11087500,19.53
8/21/2008,19.06,19.18,18.87,19.11,16995100,19.11
8/20/2008,19.57,19.65,19.1,19.17,16336900,19.17
8/19/2008,19.78,19.91,19.41,19.42,12837300,19.42
and my code is
// csv_read.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <cstring>
#include<stdio.h>
#include<stdlib.h>
#include <vector>
#include <iostream>
using namespace std;
struct yahoo_data
{
char date[9];
double open;
double high;
double low;
double close;
float volume;
double adj_close;
};
vector<yahoo_data> yhoo;
typedef struct yahoo_data data_format;
int _tmain(int argc, _TCHAR* argv[])
{
FILE *fp;
fp=fopen("table.csv","r");
char bufferCustNum[9];
char bufferCost[40];
int i = 0;
data_format temp;
cout<< "before while"<<endl;
while( fgets(bufferCustNum,sizeof(bufferCustNum),fp) != NULL)
{
cout<< "i = "<< i << endl;
if(i>0){
strcpy(temp.date, strtok(bufferCustNum,","));
cout<< temp.date<<endl;
/*
strcpy(temp.open, (double)strtok(NULL,","));
strcpy(temp.high, strtok(NULL,","));
strcpy(temp.low, strtok(NULL,","));
strcpy(temp.close, strtok(NULL,","));
strcpy(temp.volume, strtok(NULL,","));
strcpy(temp.adj_close, strtok(NULL,","));
*/
temp.open = (double)atof(strtok(NULL,","));
cout<< "after open"<<endl;
temp.high = atof(strtok(NULL,","));
cout<< "after high"<<endl;
temp.low = atof(strtok(NULL,","));
cout<< "after low"<<endl;
temp.close = atof(strtok(NULL,","));
cout<< "after close"<<" "<<temp.close<<endl;
// cout<< atof(strtok(NULL,","))<<endl;
temp.volume = (int)atof(strtok(NULL,","));
cout<< "after adjust close"<<endl;
temp.adj_close = atof(strtok(NULL,","));
cout<< "before push"<<endl;
yhoo.push_back(temp);
cout<< "before push"<<endl;
}
++i;
}
for (int j=0; j<10; j++)
{
}
return 0;
}
There is some unnecessary part in the code but it should compile under g++. I get some memory leaks... Anyone have idea?