Alternative declaring the file pointer instead FILE * pointer in C - c

I am looking for a alternative for declaring the file pointer without using "FILE * pointer", so I mean without using the IO standard library function, maybe as a function.
With the library its easy but how could the function look like without the library.
Can somebody give an advice?

glibc includes a low-level file API for handling files with int file descriptors.
In fact, I think if you were to cast a FILE* to an integer in the glibc implementation, you would obtain a file descriptor usable with the low-level file API, and vise versa.

Related

Is it necessary to free the file pointer when working with a file in the C language?

I'm studying files in C and I was curious if it's necessary to use the free() function in the file type pointer, if not, I'd like an explanation.
FILE *p;
Since p is a pointer shouldn't it be free anyway?
The primary 'allocator' for file streams (FILE *) is fopen() (though there are some others1).
The primary 'deallocator' for file streams is fclose()2.
You should not use free() to attempt to release a file stream. You should only free memory allocated 'as if' with malloc() or its relatives. It is important to know when you create/access a resource in your program, what is the correct way to release that resource. The manual pages will tell you.
1 freopen(), fdopen(), fmemopen(),
popen().
2 pclose(). Note that file streams created with popen() should be released with pclose(), not fclose().

How to convert a file stream to string in C?

I'm using a system function which writes the output information into a stream of file pointer.
func(FILE *__fp)
I need to use this information in my program rather than printing this out to a file. For that I thought of creating a tmpfile() and writing to it then reading back from it. But is there a better way to get this information?
There are OS-specific solutions to writing to a memory buffer instead of a file, like for example the POSIX fmemopen or open_memstream (both which should be very useful considering your linux tag).
You can also change the internal buffer to your own with setvbuf.
On an unrelated note: Symbols starting with a leading underscore and followed by another underscore (like for example your __fp argument) are reserved. Such symbols may only be used by "the implementation", i.e. the compiler and library.

Call a function or a program when using the C-function fopen

I have a situation in C where I would like to call a c-function when calling fopen. This means I would like to have a "virtual file" of some sort. When I use fopen on this "virtual file" I would like to call a function to produce the data in the file.
Is this possible?
Thanks!
There isn't a direct way to call a function to produce output. However, you can call another process using popen(), which may be sufficient for your needs.
This means I would like to have a "virtual file" of some sort. When I
use fopen on this "virtual file" I would like to call a function to
produce the data in the file.
To do that, you'd need to write your own file system. Lucky for you, other people have done the hard part: take a look at FUSE. For example, you could write a file system where the "files" are really RSS feeds. You could then use standard file calls to read the data form those feeds.
Now, whether you should take this approach is a different question. If you have control of the code that's reading the file, it'd probably be easier to just have it call the appropraite data-providing function than to require installing a custom file system.
In standard C that is not possible, AFAIK,
If you use a system with the GNU Glibc (such as GNU/Linux) you can have custom streams, notably thru fopencookie.
Notice that the standard C++ library also provides (its own variant of) streams, and you could have your own.
On GNU/Linux, the kernel enables you also to provide a file-system in user space with FUSE

Converting a Lua file object to a C FILE*

I'm building a wrapper using LuaJIT and FFI. I have a C library with a function that takes a FILE* as a parameter. In a Lua function within which I open a file using io.open(). Is there a way to cast, convert, or extract from the Lua file object to a C FILE*? This seems like it should be straightforward but I can't find a solution. Thanks in advance.
LuaJIT head will automatically convert a Lua file handle to a FILE * (well, void *).
http://www.freelists.org/post/luajit/an-admittedly-lazy-programmer-question,4

Binary compatibility of FILE*

I am designing C library which does some mathematical calculations. I need to specify serialization interface to be able to save and then load some data. The question is, is it correct (from binary compatibility point of view) to use FILE* pointer in the public API of library?
Target platfoms are:
Linux x86, x86_64 with gcc >= 3.4.6
Windows x86, x86_64 >= WinXP with VS >= 2008sp1
I need to be as much binary compatible as it possible, so at the moment my variant is the following:
void SMModuleSave(SMModule* module, FILE* dest);
SMModule* SMModuleLoad(FILE* src);
So I am curious if it is correct to use FILE* or better switch to wchar*/char* ?
I don't agree with ThiefMaster: there's no benefit in going native (ie using file descriptors of type int on linux and handles of type void * on windows) when there's an equivalent portable solution.
I'd probably go with FILE * instead of opening the files by name from within the library: It might be more of a hassle for library users, but it's also more flexible as most libc implementations provide various ways for file opening (fopen(), _wfopen(), _fdopen(), fdopen(), fmemopen(),...) and you don't have to maintain seperate wide-char APIs yourself.
I'd use neither but let the user pass a file descriptor as an int.
Then you can fdopen() it in your code to get a FILE*.
However, when using windows it might not be the best solution even though it does have some helper functions to get a numeric file descriptor.
However, passing a FILE* or a const char* should be fine, too. I'd prefer passing a filename as it's less code to write if a library takes care of opening/closing a file.
Yes it is correct, from a stable binary interface perspective, to use FILE * here. I think perhaps you're confusing this with using FILE as opposed to a pointer to it. Notice that your standard library's fopen, fgets, etc. functions all use (both as arguments and return values) the FILE * type as part of their public interfaces.
A FILE * is a standard ANSI/ISO C89 and C99 (even K&R) type. It is a portability dream and I'd prefer it over anything else. You're safe to go with it. It won't get any better than that.

Resources