tags:

views:

88

answers:

4

Hi all, 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. =]

+2  A: 

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

Anders K.
+1  A: 

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

unsigned linesRead;
unsigned nextBufLine; 
karlphillip
Specifying unsigned without a data type defaults to int: http://stackoverflow.com/questions/2099830/unsigned-keyword-in-c
Saxon Druce
Correct, is this considered bad practice though?
00dium
It's usually best to go with whatever will be most obvious to most people, for when others read the code - so yes I would be explicit and use `unsigned int` instead.
Saxon Druce
makes sense, thanks :)
00dium
+3  A: 

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

Saxon Druce
no i haven't, is it possible that some other library i have included has redefined it?
00dium
Possibly - try isolating your code from any other headers which your code is including (either in the header you have defined this type in, or the cpp file which is including this header).
Saxon Druce
turns out 'i did' have a macro redefining FILE, included from another file, rather stupid in hindsight consideringexpected specifier-qualifier-list before string constant <-- STRING CONSTANT???? =/Cheers for the help, nice community here.
00dium
+2  A: 

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

nonVirtualThunk