Hi all, I'm new to c++ and currently i need to write a program to open csv file and store all data into a vector of double. I have read through the forum and i manage to write a program that can store data in a vector of string which is not suitable for my case because my algorithm function is expect a double type data. So I need help how to fix this problem. How to store the numerical data in vector of double?
Hi shacktar, thanks for your reply. I have changed the getline to operator >> and there is an error when i'm compiling the code.
the error:
error C2440: '=' : cannot convert from 'std::string *' to 'double *'
Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
Is my pointer problem?how can i pass the csv data into my InputSignal function?
Why is your double a pointer anyways? That doesn't make any sense.
signal /*this a double* */ = &item/*this is a string* */;
You're trying to assign a pointer that's supposed to point to stuff like "hello" to a pointer that's supposed to point to stuff like 2.712 - I don't get why you're doing this.
Yeah, that compilation error should have shown up with the original code. Maybe it was declared double* in anticipation of actually receiving a double from the file.
i have make the following correction but there is an error C2065: 'lineStream' : undeclared identifier shown up:
std::vector<string> file; changed to std::vector<double> file;
string item; changed to double item;
getline(lineStream, item); changed to lineStream>>item;
Hanst99:
i know my pointer is double and from my original code the pointer is pointing a string type. That's why i want to read my csv file into double instead of string so that my pointer can point to same data type and pass to InputSignal function.
I'm apologize that i might asking some silly question because i'm totally new to c++. Sorry about that and hope u guys can help me out. Thanks.
What I am expecting is the *signal should contain only the last three column " 809 0.008 0 -0.002" data. Do I need to create another array and use the *signal pointing at array instead of &item?
while (getline (inFile, line))
{
istringstream lineStream(line);
double item;
lineStream>>item;
file.push_back (item);
signal = &item;
InputSignal(signal);
}
then it makes sense that it's only grabbing the first column. You can make an array of doubles and extract the columns into there.
1 2 3 4 5 6 7 8 9 10 11 12 13 14
while (getline (inFile, line))
{
istringstream lineStream(line);
double data[6]; //one double for each column
//extract each column for this line
for(int i = 0; i < 6; i++)
{
lineStream>>data[i];
}
...
}
data[3], data[4], and data[5] would contain the last three columns.
Hi, after trying my codes for few days, i have encountered a problem. I couldn't load all columns for data into an array.Please help me to check my codes where is the problem.
Thanks.
bool CSOINN::InputSignal(constdouble *signal){
FindWinnerAndSecondWinner(winner, secondWinner, signal);
}
bool CSOINN::FindWinnerAndSecondWinner(int &winner, int &secondWinner, constdouble *signal){
dist = Distance(m_nodeInfo[i].m_signal, signal);
}
double CSOINN::Distance(constdouble *signal1, constdouble *signal2)
{
int i;
double sum;
double error;
if (signal1 == NULL || signal2 == NULL) return 0.0;
error = 0.0;
sum = 0.0;
for (i=0; i<m_dimension; i++)
{
sum += (signal1[i]-signal2[i])*(signal1[i]-signal2[i]);
}
return sqrt(sum)/(double)m_dimension;
}
How can i use a pointer to store all data from the csv.file's column and pass to these functions?hope you can help me out. I have been stuck for few days ago..
Thanks..
These results are strange. Assuming those are the lines it's reading, then there shouldn't be a problem with the output here. Did you print line (and lineStream.str()) to make sure they match with the file? Also, I assume j is declared somewhere?
signal = &item[j];
There's a problem here because signal will point to the last member in the array after that loop is over. That means signal[i] where i >= 1 would mean an array out of bounds. If you want to access all three elements of the array, then you can just pass data into InputSignal.
the j has been declared and I forgot to paste it in previous post. Thanks for your reminding and i realized the signal only point to the last member of array.
Can you show me how to pass 1 row of data frm csv file into InputSignal function? Because my idea is i want to pass row by row of data into InputSignal but i have no idea how to do it. For example, data 1 2 3 4 5 should pass into InputSignal then next data will be 6 7 8 9 10.
InputSignal would have the same signature as you have it:
bool CSOINN::InputSignal(constdouble *signal)
There (and any function you pass signal to), you can do signal[i] to access members of item for i from 0 to 4, inclusive.
What's bugging me though is the garbage that's being printed out when j is 1 or 2. At this point, I would say that that would happen if the line being read is not matching the file.
That code looks right. You should try printing line before passing it to the stringstream to make sure the string is what you expect. Besides that, all I can think of is there's some kind of undefined behaviour (division by zero, dereference null pointer, etc.) that's happening before the population of the item array that's causing the corruption. Posting the rest of the relevant code may be helpful.
Well, that would cause the stringstream to put garbage into all but the first double. If your line is this:
1, 2, 3, 4, 5
then lineStream >> item[1] would try to put a comma into item[1] and the stringstream would be in a bad state.
To read a particular line from the file, you should do this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
while (getline (inFile, line))
{
//knowing there are 5 entries
double item[5];
string dataEntry; //a particular entry as a string
istringstream lineStream(line);
for(int i = 0; i < 5; i++)
{
//get one entry
getline( lineStream, dataEntry, ',');
lineStream >> ws; //remove whitespace after the comma and before the next entry
istringstream( dataEntry ) >> item[i];
}
pSOINN->InputSignal(item);
}
Hi shactar and ne555, the code is working finally. Thank you so much for spending your time to help me.
One more question is:
How can i just read the last three columns of csv file and skip the 1st and 2nd columns. Because some of my csv file i do not need the 1st and 2nd columns.