I want to do something that seems impossible.
I want to write 3 files: main.c , foo.h , foo.c.
The idea is to define a structure in foo.h or foo.c so that its elements are visible in main.c. One member of the structure is of type FILE , as defined in the standard <stdio.h> header. But functions and macros <stdio.h> should remain invisible inside the main.c file.
The solution must comply with C standard (C99 and / or C11) and portable. Is it possible?
The header foo.h is included in both: main.c and foo.c.
The standard header <stdio.h> # is included only in foo.c , but not in main.c.
I want to declare a struct as follows:
struct myfile_s { FILE *F; char *filename; char buff[100]; } myfile;
For further, only the member FILE *F is important. The myfile structure will be initialized and modified by the functions displayed in foo.c.
The goal is to perform file operations on myfile through extern functions declared in foo.h , which in turn must be defined in foo.c.
File operations include the standard <stdio.h> functions, but these functions must be hidden in the main.c file. However, I want to get direct access from main.c to myfile members. This would mean that the definition of struct myfile_s should be done in foo.h (that is, it cannot be โdelayedโ with the trick of using incomplete structure declarations, such as struct myfile_s ; ).
In short, I would like the FILE type to be visible in main.c and / or foo.h , but keep the rest of the <stdio.h> declarations hidden to them.
Another limitation: the idea of โโdirect copying at hand is the FILE definition provided by <stdio.h> , which is undesirable because the code must be portable.
My first attempt was to hash a struct myfile by injecting it inside foo.c.
The code was:
foo.h
struct myfile_s; struct myfile_s myfile; extern void startfile(struct myfile *f);
foo.c
#include <stdio.h> #include "foo.h" struct myfile_s { FILE *F; char *filename; char buff[100]; } myfile; void startfile(struct myfile *f) { // Do some stuff... }
main.c
#include "foo.h" int main(void) { myfile.F = NULL;
The latency struct myfile_s announces the need to include <stdio.h> in foo.h , so the header works fine, but the structure type remains incomplete, so access to myfile.F cannot do main.c.
So: How can I access myfile.F?
I tried another way, which of course also gives an error:
foo.h
struct myfile_s { FILE *F; char *filename; char buff[100]; } myfile; extern void startfile(struct myfile *f);
foo.c
#include <stdio.h> #include "foo.h" void startfile(struct myfile *f) { // Do some stuff... }