将fpos_t转换为int或char。

时间:2022-02-27 02:56:33

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

我正在使用一个函数,该函数使用一个文件长度的位操作: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

当我尝试将其转换为int或char,或尝试在其上进行算术操作时,它会失败,因为以下编译错误:错误:期望的整数使用的聚合值。

3 个解决方案

#1


5  

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.)

你滥用类型。首先,它不代表长度。它代表了一个位置。其次,它只用于调用fsetpos。你不是要在它上面做算术,因为它不一定代表一个数字类型。它包含您的库需要能够执行fsetpos操作的任何信息。在库的实现中,fpos_t似乎是一个聚合类型,例如结构。(您可以在头文件中检查定义,但是不要依赖于您在那里发现的任何东西;在其他平台或标准库的将来版本中,它可能会有所不同。

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.

至于下一步,你可以考虑问一个更直接的问题,即当你想出一个在fpos_t上执行bitwise操作的想法时,如何解决你正在处理的任何问题。

#2


2  

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.

当我试图这样做时,我得到了相同的错误:char aux = (char)flen & 15, flen是fpos_t。我在这里找到了一个解决方案:http://dsmarkchen.blogspot.com.br/2008/08/fpost.html。应该是:char aux = flen。__pos & 15。

#3


0  

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.

这听起来好像你想要以整数来处理当前文件的位置。如果是这样,您可能希望使用ftell/fseek(或他们的64位兄弟),而不是fgetpos/fsetpos。两者都提供了类似的操作,但是ftell/fseek使用的是积分值,而fgetpos/fsetpos的工作是一种意图抽象的结构。

#1


5  

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.)

你滥用类型。首先,它不代表长度。它代表了一个位置。其次,它只用于调用fsetpos。你不是要在它上面做算术,因为它不一定代表一个数字类型。它包含您的库需要能够执行fsetpos操作的任何信息。在库的实现中,fpos_t似乎是一个聚合类型,例如结构。(您可以在头文件中检查定义,但是不要依赖于您在那里发现的任何东西;在其他平台或标准库的将来版本中,它可能会有所不同。

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.

至于下一步,你可以考虑问一个更直接的问题,即当你想出一个在fpos_t上执行bitwise操作的想法时,如何解决你正在处理的任何问题。

#2


2  

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.

当我试图这样做时,我得到了相同的错误:char aux = (char)flen & 15, flen是fpos_t。我在这里找到了一个解决方案:http://dsmarkchen.blogspot.com.br/2008/08/fpost.html。应该是:char aux = flen。__pos & 15。

#3


0  

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.

这听起来好像你想要以整数来处理当前文件的位置。如果是这样,您可能希望使用ftell/fseek(或他们的64位兄弟),而不是fgetpos/fsetpos。两者都提供了类似的操作,但是ftell/fseek使用的是积分值,而fgetpos/fsetpos的工作是一种意图抽象的结构。