Sirhc
Sirhc

Reputation: 538

Is it possible to store a FILE * in a user defined struct?

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 fileName[MAX_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 */
}FileType;

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. =]

Upvotes: 2

Views: 253

Answers (4)

nonVirtualThunk
nonVirtualThunk

Reputation: 2852

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

Upvotes: 2

Saxon Druce
Saxon Druce

Reputation: 17624

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

Upvotes: 3

karlphillip
karlphillip

Reputation: 93410

What data type is it? char , int ...

unsigned linesRead;
unsigned nextBufLine; 

Upvotes: 1

AndersK
AndersK

Reputation: 36082

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

Upvotes: 2

Related Questions