   I need help creating a custom filestream / filling _iobuf correctly in C
  The title of this thread describes my problem very accuratley. I need to fill out an _iobuf object so that I can access it with fread, fscanf etc.A friend of mine tried doing this via educated guessing - it ended up working on some systems and failing on others, so please only reply if you know exactly how to do this.I concidered posting this on a pure C forum, but I am not registered on one right now so I hope someone here can help.   
  These are implementation dependant, there is no way (AFAIK) to make one that is guarenteed to work on everything.   ;  Ok, well perhaps I need to clarify what I am trying to do. I want to be able to read from a specific buffer just as if it was a regular file.If there is any way to do this with stdio I would be very happy to know!   ;  If you're trying to reverse engineer something that isn't documented and you can't google an obvious answer, you'll have to put many hours of effort in to figure everything out & test it.  However, if you're doing something that is not documented then there is probably a very good reason for not allowing you to do it and you're playing with fire.  Also note that if something is not documented to users then different implementations will use different behind the scenes techniques to optimise their code and as you have no control over this you are in big trouble because if you get something working today, it could break when a new implementation of a library comes out tomorrow.Best idea I can suggest is to write your custom buffer to the disc and then open as a file and everything will work just fine.  Another option is to write your own version of all the functions you want to use (this is what us "old time" game programmers used to do in the past, but I think we're all happy to work with the libraries now because it's pretty painful).;  Yes, implementing a wrapper is very easy in this case but throws away the benefits of being able to use it with older, FILE*-reliant libraries.   ;Quote:Original post by jeff0Ok, well perhaps I need to clarify what I am trying to do. I want to be able to read from a specific buffer just as if it was a regular file.If there is any way to do this with stdio I would be very happy to know!Still not possible in a standardized way (AFAIK).  Without knowing which platform you're targeting, about all I can advise is to jump into your stdlib implementation and do what they do - take a look at the struct FILE is typedefed to, and preferibly grab the source for the implementation of your fopen() to sanity check what the different fields of that struct are used for.As an aside:ibook-g4:~ panda$ fgrep "iobuf" /usr/include/*ibook-g4:~ panda$ man -k iobufiobuf: nothing appropriateibook-g4:~ panda$ _So much for this platform being targetable by whatever structs you're focused on now :-).If your platform dosn't have an open source implementation of fopen for viewing, and permanently switching platforms isn't an option, you're going to need to rely heavily on documentation.  Using an open source platform for testing the more generic parts of such a class, and porting to the closed source platform once you've confirmed it's working, may also work well.  Checking any available documentation for the closed source platform is also a must.If you're masochistic, it should be possible to fill the struct out in a workable manner using trial and error (what your friend did it sounds like).  Fixing it on other platforms would involve more stringent unit testing, or otherwise finding the source of the problem (inspecting the struct after every operation to confirm what it's doing, for example).  But reaching this point, it might be easier to port the existing codebase to a custom layer/function set which works atop fopen & co (and your own buffer manipulation routines).  Depends on how big it is and what all it uses, of course.   ;  Hmm, thank you for your opinion MaulingMonkey. I've been thinking about an alternative solution for a while now: storing my data in a temporary file and then opening it with fopen.That way I can still use my old library. On the other hand I might just make a file wrapper and re-write it all.   ;  You're not supposed to access the members of the _iobuf structure. FILE* is supposed to be opaque. A pure C forum would tell you these things as well - actually, they would probably be somewhat mean about it. On comp.lang.c you would likely be informed in short order that there is no such thing as an _iobuf in the standard and therefore there is no such thing in C. If you're running windows and you want to associate a file HANDLE with a FILE pointer, investigate the _open_osfhandle, _get_osfhandle and _fdopen (etc) functions. Here are a couple of links that might help. Adding Console I/O to a Win32 GUI App (Windows Developer Journal, December 1997)A Handy Guide To Handling HandlesHere's a snippet from the WinDev article. The HANDLE doesn't have to be standard out, it could be a file instead. HANDLE lStdHandle;int hConHandle;FILE *fp;lStdHandle = GetStdHandle(STD_OUTPUT_HANDLE);hConHandle = _open_osfhandle((long)lStdHandle, _O_TEXT);fp = _fdopen( hConHandle, "w" );;Quote:Original post by LessBreadIf you're running windows and you want to associate a file HANDLE with a FILE pointer, investigate the _open_osfhandle, _get_osfhandle and _fdopen (etc) functions. Here are a couple of links that might help.Yes, I did know u could convert a file handle into a FILE* but that wasn't really what I had intended.;  Yay! I found my soulution on the C Board forums. The first person to reply on that thread gave me the answer I had been looking for a long time :) !Just thought I'd inforum u guys since you apparently didn't know how to do this.   