c – Is it possible to store a FILE * in a user defined struct? – Education Career Blog

first post here so be nice 😉

Is it possible to store a FILE * in a struct, i see no reason why not but the following code wont compile, i can’t seem to store a reference to file pointer either.

typedef struct fileType
    FILE * file;
    char fileNameMAX_FILENAME_LEN;
    unsigned linesRead;
    unsigned nextBufLine;           /* next line to be inserted/removed in the buffer */
    pthread_mutex_t * mtxFile;      /* mutex controlling access to this file */

My compiler doesn’t seem to recognise the type ‘FILE’ throwing this error at that line, and of course i have included stdio.h in the header

error: expected specifier-qualifier-list before '(' token

Basically I’m writing a program which spawns a a series of child process, taking turns to read lines from a file and inserting them into a circular buffer where they are read by a another set of child processes, encrypted and written to a new file (line by line). It is a requirement that the parent open and closes the file.

I’m permitted to use globals for this but want to avoid it if possible, thanks for any replies. =


Do you have a macro somewhere which is redefining FILE or file as something else?


If you include <stdio.h> it should be fine to have a FILE* member in your struct.


There isn’t anything wrong with storing a FILE* in a struct, and given that the error message mentions a ‘(‘ I suspect the problem could potentially be in some other part of your code (since there isn’t a left parenthese in the code you posted). If you post more of the code we might be able to help you better. Given what you have there my only other thought is that you missed an include for the pthread_mutex_t


What data type is it? char , int …

unsigned linesRead;
unsigned nextBufLine; 

Leave a Comment