#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!

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;
}

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

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?

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?

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 posted L7Sqr's code is far beyond your level.

commented: Rude +0
commented: harsh reality +8

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.

commented: should know how to behave in a public place, dont show attitude here. -2

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.

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 from L7Sqr? 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.

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?

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.