Conversion: uid_t to string, off_t to string

11,554

Solution 1

Both are defined as arithmetic types (http://www.opengroup.org/onlinepubs/009695399/basedefs/sys/types.h.html), and in practice are positive and integral. So you can just cast to unsigned long long, and use sprintf with "%llu" to convert to string.

Solution 2

size_t and off_t are just unsigned integral types. (Edit: off_t is a long? See, the lesson is, check your headers!)

So use sprintf (or whatever) to convert them using the "%i" format specifier.

On edit: crap, you changed size_t to uid_t while I was answering. uid_t is defined in types.h; look there. (It's also an unsigned integral type, but an unsigned short.)

Solution 3

Linux' snprintf() supports the 'z' format specifier for values of type size_t. Not sure how portable this is, you'll need to inspect the "CONFORMS TO" section closely.

For off_t, you might need to cast to the largest unsigned integer type, i.e. unsigned long and use a "lu" specifier.

Share:
11,554
israkir
Author by

israkir

Without music, life would be a mistake...

Updated on June 28, 2022

Comments

  • israkir
    israkir almost 2 years

    I am currently writing a systems programming homework and in one part i need to get some information of a file in a directory.

    for the stat of file, we have ctime() function which converts time_t type to string and returns a pointer to it.

    but how about the uid_t and off_t types? I searched through to internet and couldnt find any function.. Or if there does not exist any function, can you tell me how to implement such a function please?

  • Matthew Flaschen
    Matthew Flaschen about 15 years
    I don't believe they're guaranteed to be those types on all platforms.
  • Matthew Flaschen
    Matthew Flaschen about 15 years
    I think the actual lesson is that these are different sizes on different systems.
  • Matthew Flaschen
    Matthew Flaschen about 15 years
    Why can't you just widen the values to unsigned long long?
  • tpdi
    tpdi about 15 years
    Yes, which is why I wrote "check your headers". Emphasis on "your". :)