954,480 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Convert string into double array

#define _LL_BUFFSIZE_ 2048
string lastLine;
lastLine.clear(); // regardless, zero out our return string
char buff[_LL_BUFFSIZE_]; // our temporary input buffer
data_filet.seekg (0, ios::end); // go to end of file
int length = data_filet.tellg(); // find out how large it is
data_filet.seekg(length-min(length,_LL_BUFFSIZE_),ios::beg); // seek back from end a short ways
// read in each line of the file until we're done
buff[0]=0;
do{if (!isspace(buff[0]) && buff[0] != 0)
lastLine = buff;
} while (data_filet.getline(buff, _LL_BUFFSIZE_));
cout <<"last ="<< lastLine << endl;//Last line of t stored as 
cout << typeid(lastLine).name() << endl;


I use this code to read the last line of a csv file and the value of lastLine is
"29.27,83.37,39,43" etc. The number of values changes everything. I need to find a way to store this string as an array of those numbers. Where say t[0]=29.27 and t[1]=83.83 and so on. Thanks!

JordanHam
Light Poster
41 posts since Jan 2011
Reputation Points: 8
Solved Threads: 0
 

The conversion can be tricky (does your csv allow quoted commas?) but you could just translate all ',' to ' ' and parse using a stringstream. For example:

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

int main () {
    std::string s = "123.45,213.76,234.16";
    std::size_t pos = 0;
    std::vector< double > vd;
    double d = 0.0;

    // convert ',' to ' '
    while (pos < s.size ())
        if ((pos = s.find_first_of (',',pos)) != std::string::npos)
            s[pos] = ' ';

    std::stringstream ss(s);
    while (ss >> d)
        vd.push_back (d);

    for (int i = 0; i < vd.size (); ++i)
        std::cout << "vd[" << i << "] = " << vd[i] << std::endl;
    return 0;
}
L7Sqr
Practically a Master Poster
656 posts since Feb 2011
Reputation Points: 201
Solved Threads: 123
 

You need to parse the line. Find each (,) and
1) Convert the value to float and store it.
2) Search for a comma
3) If found, skip over it and go back to 1

WaltP
Posting Sage w/ dash of thyme
Moderator
10,505 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
 

The conversion can be tricky (does your csv allow quoted commas?) but you could just translate all ',' to ' ' and parse using a stringstream. For example:

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

int main () {
    std::string s = "123.45,213.76,234.16";
    std::size_t pos = 0;
    std::vector< double > vd;
    double d = 0.0;

    // convert ',' to ' '
    while (pos < s.size ())
        if ((pos = s.find_first_of (',',pos)) != std::string::npos)
            s[pos] = ' ';

    std::stringstream ss(s);
    while (ss >> d)
        vd.push_back (d);

    for (int i = 0; i < vd.size (); ++i)
        std::cout << "vd[" << i << "] = " << vd[i] << std::endl;
    return 0;
}


Thank You, this code works for me but when I try and sub the lastLine I get from my code in place of your s it does not work? Why is this?

JordanHam
Light Poster
41 posts since Jan 2011
Reputation Points: 8
Solved Threads: 0
 
You need to parse the line. Find each (,) and 1) Convert the value to float and store it. 2) Search for a comma 3) If found, skip over it and go back to 1

I dont know how to do this. Strtok? and a for loop?

JordanHam
Light Poster
41 posts since Jan 2011
Reputation Points: 8
Solved Threads: 0
 
Thank You, this code works for me but when I try and sub the lastLine I get from my code in place of your s it does not work? Why is this?


Probably because you tried to add code you don't understand and expect it to work without modification. It's obvious from the code you postedL7Sqr's code is far beyond your level.

WaltP
Posting Sage w/ dash of thyme
Moderator
10,505 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
 
Probably because you tried to add code you don't understand and expect it to work without modification. It's obvious from the code you posted L7Sqr's code is far beyond your level.

It is obvious to me that common decency is far beyond your level.

JordanHam
Light Poster
41 posts since Jan 2011
Reputation Points: 8
Solved Threads: 0
 

If each entry in the file is comma separated(every field separated by a comma, no new line char or other whitespace), then you can read the file using getline() with the comma char as the delimiting char to "find" each comma. After each read from the file into a string using getline() you can convert that string into type double using a stringstream (although there are other, less "suitable" ways to convert from string to numerical types, too) and place the numerical value into an array of type double if that's what you want to do.

Lerner
Nearly a Posting Maven
2,382 posts since Jul 2005
Reputation Points: 739
Solved Threads: 396
 
It is obvious to me that common decency is far beyond your level.


Common decency? I gave you an answer that will work extremely well. You ignored it.

Can you explain the code fromL7Sqr? If so, you should know why didn't it work? If not, that's why it didn't work. Has nothing to do with common decency.

Look at your post:
Thank You, this code works for me but when I try and sub the lastLine I get from my code in place of your s it does not work? Why is this?
Common decency dictates you explain what happened, not force us to use psychic powers to figure out what you did wrong. Show us.

WaltP
Posting Sage w/ dash of thyme
Moderator
10,505 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
 

My post was meant to illustrate a point, not provide an exact solution. Forgive me if I did a poor job explaining what is happening there.

In essence, I've done the same thing a WaltP suggests except that I add the step of converting commas to spaces to allow standard facilities to do the 'heavy lifting' for me.

To reiterate the point in posts above, what exactly did not work for you? What output did you expect - what did you get?

L7Sqr
Practically a Master Poster
656 posts since Feb 2011
Reputation Points: 201
Solved Threads: 123
 

for anyone that may ever need help. This find the last line of a csv and parses it

#define _LL_BUFFSIZE_ 2048
	string lastLine;
	lastLine.clear(); // regardless, zero out our return string
	char buff[_LL_BUFFSIZE_]; // our temporary input buffer
	data_filet.seekg (0, ios::end); // go to end of file
	int length = data_filet.tellg(); // find out how large it is
	data_filet.seekg(length-min(length,_LL_BUFFSIZE_),ios::beg); // seek back from end a short ways
	// read in each line of the file until we're done
	buff[0]=0;
	do{if (!isspace(buff[0]) && buff[0] != 0)
	lastLine = buff;
	}	
	
	while (data_filet.getline(buff, _LL_BUFFSIZE_));
	cout <<"last ="<< lastLine << endl;//Last line of t stored as 
	cout << typeid(lastLine).name() << endl; 


	char *a=new char[lastLine.size()+1];
	a[lastLine.size()]=0;
	memcpy(a,lastLine.c_str(),lastLine.size());
	char * pch;
	printf ("Splitting string \"%a\" into tokens:\n",a);
	cout<<"Problem"<<endl;
	pch = strtok (a,", ");
  
	while (pch != NULL)
	{
		printf ("%s\n",pch);
		pch = strtok (NULL, " ,-");
	}


Thanks L7

JordanHam
Light Poster
41 posts since Jan 2011
Reputation Points: 8
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: