ifstream & .csv

I have a program that needs to perform the same function on several .csv files. My question is if there is any way to replace just the file name in the ifstream function, and possibly run this as a loop, or something of that nature. I have tried several different ways of doing this to no avail.
Thanks.
int symbol::parseCSV(double *ptr)
{
int k=0;
string quote;
size_t length;
double sum=0,avg;
char store[200];

ifstream file ("ratesfx-jpy-nzd.csv.txt");
while(file.good())
{
getline(file,quote,',');
length=quote.copy(store,8);
store[length]='\0';
*(ptr-2)=atof(store);

k++;
*ptr++;
}
return k;
}
I think maybe you want to look at open() and close()

1
2
3
4
5
6
7
8
9
10
11

std::ifstream ifs;

ifs.open("ratesfx-jpy-nzd.csv-1.txt");
// .. some stuff
ifs.close();


ifs.open("ratesfx-jpy-nzd.csv-2.txt");
// .. some stuff
ifs.close();
Last edited on
Some snippets just make me feel uncomfortable. This is one of them.

Particularly, but not limited to, this:
*(ptr-2)=atof(store);
There should be a hall of fame somewhere for such like.. that's a real cracker :o)
Some snippets just make me feel uncomfortable. This is one of them.

Particularly, but not limited to, this:
*(ptr-2)=atof(store);


I would be interested to know why this makes you uncomfortable. This is part of basically the first program I have ever written, so I love tips on how to improve. The reason I did this was because the first couple of lines in the .csv file are header information, so I had to skip that stuff to get to what I need. I also did an average part to keep out any outliers. The full excerpt is:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
int symbol::parseCSV(double *ptr)  //the pointer points to a double array in main
{
int k=0;
string quote;
size_t length;
double sum=0,avg;

char store[200];

for(int s=0;s<400;s++)
*(ptr+s)=0;


if(_s==1)
{
ifstream file ("ratesfx-jpy-nzd.csv.txt");
while(file.good())
{
getline(file,quote,',');
if(k>1)
{
length=quote.copy(store,8);
store[length]='\0';
*(ptr-2)=atof(store);
if(k>2)
{
if(*(ptr-2)>((.6*avg)+avg)||*(ptr-2)<(avg-(.6*avg)))
{
k--;
*ptr--;
}
}
sum=sum+*(ptr-2);
avg=sum/(k-2);
}
k++;
*ptr++;
}
}
else if(_s==2)

//...repeat for several symbols

k=k-2;
return k;
}
Topic archived. No new replies allowed.