I can make a file pointer write to a file with fopen(). But can I make a file pointer that will make it so calling functions such as fputc or fprintf will write to a pointer in memory? An example of this is ByteArrayOutputStream in java. Also: could I run it in reverse, where a library needs a file pointer to read from, so I allocate memory, and make a new file pointer that will read from this memory location but return EOF when the size of the chunk runs out? (like ByteArrayInputStream in Java). Is there a way to do this in C? For example:
FILE *p = new_memory_file_pointer(); fprintf(p, "Hello World!\n"); char *data = get_written_stuff(p); printf("%s", data); //will print Hello World!
&& / ||
char s[] = "Hello World!\n"; FILE *p = new_memory_file_pointer_read(s, sizeof(s)); char *buffer = (char *)malloc( 1024*sizeof(char) ); fread((void *)buffer, 1, sizeof(s), p); printf("%s", buffer); //prints Hello World!
EDIT: To those reading this question years later, in addition to the accepted answer, you should look at open_memstream(3)
, which behaves more like these Java classes than fmemopen
does.
The pointer points to a struct _IO_FILE that describes the open file.
The "file position pointer" is a pointer in the sense of the indexing-pattern: It refers/points/marks/identifies to a specific index/position in a file.
seek() method In Python, seek() function is used to change the position of the File Handle to a given specific position. File handle is like a cursor, which defines from where the data has to be read or written in the file.
If your operating system provides fmemopen, probably it will meet your purpose.
In C++ (and you added the C++ tag) you can write a function accepting an arbitrary input/output stream. Since std::stringstream
or std::ofstream
are derived classes you can pass both of them equally into this function. An example:
#include <iostream> // for std::cout #include <fstream> // for std::ofstream #include <sstream> // for std::stringstream void write_something(std::ostream& stream) { stream << "Hello World!" << std::endl; } int main() { write_something(std::cout); // write it to the screen { std::ofstream file("myfile.txt"); write_something(file); // write it into myfile.txt } { std::stringstream stream; write_something(stream); // write it into a string std::cout << stream.str() << std::endl; // and how to get its content } }
And analogously with std::istream
instead of std::ostream
if you want to read the data:
void read_into_buffer(std::istream& stream, char* buffer, int length) { stream.read(buffer, length); } int main() { char* buffer = new char[255]; { std::ifstream file("myfile.txt"); read_into_buffer(file, buffer, 10); // reads 10 bytes from the file } { std::string s("Some very long and useless message and ..."); std::stringstream stream(s); read_into_buffer(stream, buffer, 10); // reads 10 bytes from the string } }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With