Reputation: 21335
I am interested in sharing data between Java and C++ so JNI. Currently all the C++ code expects a file so its all written in terms of std::ifstream. Rather than writing the files and reading them again, I would like to be able to just pass a char* over and have the application read from that instead somehow treating the char* as a ifstream. Is there anyway to create a ifstream that is based on a char* ?
Thanks
Upvotes: 2
Views: 1719
Reputation: 3795
Yes, it's called std::istringstream
. You can use it like this:
#include<sstream>
....
char const* s = "whatever";
std::istringstream iss(std::string(s));
int i;
iss >> i;
....
If your code expects a std::ifstream
specifically, you could change it to expect a generic std::istream
, from which both inherit, as Adrian mentioned.
Upvotes: 5
Reputation: 23916
Have your thought about using string streams - they often behave like files. Does you code really want an ifstream and not a istream. If it was written with an istream you could just drop in a stringstream/istringstream classes.
Upvotes: 2