4

I'm working with a function that uses bitwise operations with the length of a file: fpos_t flen;

When I try casting it to an int or char, or attempt on arithmetic operation on it, it fails with the following compilation error: error: aggregate value used where an integer was expected

sj755
  • 3,944
  • 14
  • 59
  • 79
  • 1
    `fpos_t` is a struct, not a number, see [here](http://dsmarkchen.blogspot.com/2008/08/fpost.html) – Seth Carnegie Dec 23 '11 at 22:52
  • @SethCarnegie It is? I'm working with some else's code. Now I'm not so sure about what I'm working on... If you know the members of the struct, put them up as an answer. – sj755 Dec 23 '11 at 22:55
  • 1
    The type of fpos_t is implementation defined - not documented. It varies from platform to platform. You can get a value with `fgetpos()` and use the value with `fsetpos()` and that is all; everything else is out of order. (The [POSIX](http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/stdio.h.html#tag_13_49) page, echoing the C standard which defines `fpos_t`, says: _`fpos_t`: A non-array type containing all information needed to specify uniquely every position within a file_.) – Jonathan Leffler Dec 23 '11 at 22:58
  • Very nice, clearly designed from stopping you from doing something silly like casting a file position to a char or int. Files usually are longer than 127 or 2GB bytes. What CRT does this? – Hans Passant Dec 23 '11 at 22:58
  • 2
    If you need the file size, use `stat()` or `fstat()`. – Jonathan Leffler Dec 23 '11 at 22:59

3 Answers3

6

You're misusing that type. First, it doesn't represent a length. It represents a position. Second, it's only meant to use in a call to fsetpos. You're not meant to do arithmetic on it because it doesn't necessarily represent a numeric type. It contains whatever information your library needs to be able to perform an fsetpos operation. In your library's implementation, fpos_t appears to be an aggregate type, such as a struct. (You can check the definition in the header files to be sure, but don't rely on whatever you discover there; it's liable to differ on other platforms or in future versions of your standard library.)

As for your next step, consider asking a more direct question about how to solve whatever problem you were working on when you came up with the idea to do bitwise operations on a fpos_t.

Rob Kennedy
  • 161,384
  • 21
  • 275
  • 467
  • I went through gdb. In linux (Fedora 15), there is a member variable called __pos, that appears to hold the length. I'm thinking maybe the author was going for this originally. – sj755 Dec 23 '11 at 23:09
  • 1
    Maybe. But there's no guarantee that the `__pos` member means what you or the other author think it means. – Rob Kennedy Dec 24 '11 at 00:52
  • 2
    I asked the guy who gave me the code. He said it compiled just fine under Solaris when he used it a few years back. I installed Solaris 11 on VMWare, compiled the program, and found that fpos_t is an integer type. In other words, fpos_t on Solaris is the same thing as fpos_t.__pos on Linux. I've since changed the code, got it to compile, and found that it works correctly. – sj755 Dec 24 '11 at 05:16
3

I was getting the same error when i was trying to do this: char aux = (char)flen & 15, where flen was fpos_t. I found a solution here: http://dsmarkchen.blogspot.com.br/2008/08/fpost.html . It should be: char aux = flen.__pos & 15.

fmilani
  • 521
  • 2
  • 9
2

It sounds like you want to work with current file position as an integer. If so, you'll probably want to use ftell/fseek (or their 64-bit brethren), rather than fgetpos/fsetpos. Both serve analogous operations, but ftell/fseek work with integral values, while fgetpos/fsetpos work with an intentionally-abstract structure.