Originally Posted by
manasij7479
But I'm curious about how you could do it in about 5 lines in C ?
Again, this is contrived because it begs the question, "what are you actually trying to do, besides just operate on a C++ stringstream"? In order to come up with a realistic alternative, the purpose of the string stream is needed.
So I did something that I think sticks closely to what you did. I replaced <stringstream> with <cstdio>, commented out 7 lines in main and added 6.
Obviously, this is a bit silly: if you are working in C++, you might as well use C++ features, but I think it does demonstrate that the task you use as an example is no simpler or easier in C++ than it is in C (so the place this would require "at least 30 lines" is...mythical):
Code:
#include<iostream>
//#include<sstream>
#include <cstdio>
using namespace std;
int main()
{
// stringstream my_stream(ios::in|ios::out);
std::string dat("Hey, I have a double : 74.79 .");
// my_stream.str(dat);
// my_stream.seekg(-7,ios::end);
const char *str = dat.c_str();
int len = dat.length() - 7;
str += len;
double val;
// my_stream>>val;
sscanf(str, "%lf", &val);
val = val*val;
// my_stream.seekp(-7,ios::end);
// my_stream<<val;
// std::string new_val = my_stream.str();
char new_val[len+64];
sprintf(new_val, "%s%.2lf", dat.substr(0, len).c_str(), val);
cout<<new_val;
return 0;
}
Output:
Hey, I have a double : 5593.54