Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Emulate a file pointer C++?

I am trying to load a bitmap from an archive. The bitmap class I have takes a character pointer to a filename and then loads it if it is in the same directory. The bitmap loading class is well tested and I don't want to mess with it too much. Problem is it uses a file pointer to load and do all of its file manipulation. Is there any way to emulate a file pointer and actually have it read from a chunk in memory instead?

Sorry if this is a bizarre question.

like image 829
Satchmo Brown Avatar asked Jul 16 '26 11:07

Satchmo Brown


1 Answers

You can also use a pipe. A pipe is a piece of memory where you can read and write using file primitives. Which is basically what you want

(Assuming POSIX Operating system)

create a pipe:

int p[2]; 
pipe(p); 

use fdopen() to turn the pipe file descriptor into a FILE*

FILE *emulated_file = fdopen(p[0], "r"); 

then write whatever you want to the write end of the pipe :

write(p[1], 17 ,"whatevereyouwant");

Now :

buf[32];
fread(&buf,1,32, emulated_file); 
cout<<buf<<endl;

willl output "whateveryouwant".

like image 190
Ben Avatar answered Jul 18 '26 02:07

Ben



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!