2

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

Sirhc
  • 518
  • 7
  • 13
  • 3
    So... can you use FILE* outside of said struct? (Yes, there *is* a reason for asking.) –  Oct 21 '10 at 02:03
  • just tried, and no i cant, can't believe i didn't try that first. so clearly structs have nothing to do with this probelm... – Sirhc Oct 21 '10 at 02:20

4 Answers4

3

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

Saxon Druce
  • 17,406
  • 5
  • 50
  • 71
  • no i haven't, is it possible that some other library i have included has redefined it? – Sirhc Oct 21 '10 at 02:12
  • 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 Oct 21 '10 at 02:14
  • turns out 'i did' have a macro redefining FILE, included from another file, rather stupid in hindsight considering expected specifier-qualifier-list before string constant <-- STRING CONSTANT???? =/ Cheers for the help, nice community here. – Sirhc Oct 21 '10 at 02:32
2

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

AndersK
  • 35,813
  • 6
  • 60
  • 86
2

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
  • 2,842
  • 20
  • 20
1

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

unsigned linesRead;
unsigned nextBufLine; 
karlphillip
  • 92,053
  • 36
  • 243
  • 426
  • 3
    Specifying unsigned without a data type defaults to int: http://stackoverflow.com/questions/2099830/unsigned-keyword-in-c – Saxon Druce Oct 21 '10 at 02:05
  • Correct, is this considered bad practice though? – Sirhc Oct 21 '10 at 02:12
  • 1
    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 Oct 21 '10 at 02:15