Here's a solution, which works with what you posted up as a test file:
Code:
// Your struct
struct Info
{
double price;
int serialNum;
std::string descript;
int order;
};
// Templated type-cast function
template <typename T, typename U>
T lexical_cast ( U src )
{
std::stringstream s;
T dst;
if ( !( s << src ) || !( s >> dst ) )
throw std::bad_cast ( "Invalid conversion" );
return dst;
}
int main ( void )
{
std::ifstream in ( "thing.txt" );
if ( !in )
{
in.close();
return 1;
}
std::string temp;
Info info;
while ( getline( in, temp, ',' ) )
{
info.price = lexical_cast< double, std::string > ( temp );
std::cout<< '\n' << "Price: " << info.price;
getline( in, temp, ',' );
info.serialNum = lexical_cast< int, std::string > ( temp );
std::cout<< '\n' << "Serial: " << info.serialNum;
getline( in, temp, ',' );
info.descript = temp;
std::cout<< '\n' << "Description: " << info.descript;
getline( in, temp, '\n' );
info.order = lexical_cast< int, std::string > ( temp );
std::cout<< '\n' << "Order #: " << info.order << '\n';
}
in.close();
return 0;
}