Quantcast read CSV format in C/C++ Text - Physics Forums Library

PDA

View Full Version : read CSV format in C/C++


leon1127
Aug31-08, 11:46 AM
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?

Hurkyl
Aug31-08, 01:03 PM
How are you identifying memory leaks? Some implementations of the STL will cache small blocks of memory. And upon program termination, if those blocks are not used, they are not explicitly deallocated -- which is fine behavior, but will confuse some memory leak detectors.

That said, I notice you forgot to close the FILE* you created.... (an fstream would automatically close upon going out of scope)

As an aside... strtok always scares me. Have you considered using stringstream to help with parsing? Or a string processing library, such as spirit from the boost (http://www.boost.org/) libraries?

jtbell
Aug31-08, 03:50 PM
Yes, a stringstream and getline() will do the job. Remember, getline() isn't just for reading lines. You can specify a different "line terminator" instead of the default '\n'. getline(mystream, mystring, ',') reads from mystream into mystring, stopping at the next ',' (and discarding the ','). Here's a sample program based on your data file:


#include <iostream>
#include <sstream>
#include <fstream>
#include <vector>
#include <string>

using namespace std;

int main ()
{
ifstream inFile ("csv.dat");
string line;
int linenum = 0;
while (getline (inFile, line))
{
linenum++;
cout << "\nLine #" << linenum << ":" << endl;
istringstream linestream(line);
string item;
int itemnum = 0;
while (getline (linestream, item, ','))
{
itemnum++;
cout << "Item #" << itemnum << ": " << item << endl;
}
}

return 0;
}


It produces the following output:



Line #1:
Item #1: 8/29/2008
Item #2: 19.54
Item #3: 19.6
Item #4: 19.28
Item #5: 19.38
Item #6: 11204900
Item #7: 19.38

Line #2:
Item #1: 8/28/2008
Item #2: 19.48
Item #3: 19.76
Item #4: 19.38
Item #5: 19.65
Item #6: 11729500
Item #7: 19.65


(etc.)

leon1127
Aug31-08, 10:27 PM
Thank you very much. that sovled my problem!!