i would like to use stream I/O functions to open a file from data that i
have already stored in memory.
eg.
char some_text[]="Test";
int main()
{
FILE* stream=fopen(some_text,"rb");
return 0;
}
This post is cross-posted to C and C++.
For C and C++, read the entire file into an array buffer. You can usually
determine the length of the file by calling fseek with SEEK_END to go to the
end of the file, ftell to get the character position (though this is not
required to return the end of file character position, but seems to under
Windows at least). Construct an array of this size, then read the entire
file into this array with fread.
In C++ you can use iostreams to the the same, but use istream::read instead
of fread, etc.
Or use std::istringstream or even std::istrstream.
std::ifstream infile("file.txt");
std::string string;
string.reserve(...);
std::copy(istream_iterator<char>(infile.rdbuf()), istream_iterator<char>(),
std::back_inserter(string));
std::istringstream instring(string);
Only thing it seems a tad inefficient as you have 2 copies of string. Of
course use create a std::auto_ptr<string> and release it afterwards, but
it's clumsy. I wish you could do
std::istringstream instring(string, std::istringstream::swapstring());