views:

1014

answers:

4

Hello,

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?

thanks in advance...

+1  A: 

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

tpdi
I think the actual lesson is that these are different sizes on different systems.
Matthew Flaschen
Yes, which is why I wrote "check your headers". Emphasis on "your". :)
tpdi
A: 

off_t is a long int: format = "%ld"

size_t is an unsigned int: format = "%u"

You can use these format in sprintf function to convert into a char*.

Jérôme
I don't believe they're guaranteed to be those types on all platforms.
Matthew Flaschen
You're right, but as they are not format for off_t or size_t, this is the only solution. This can be improved using pre processing test to know the platform.
Jérôme
Why can't you just widen the values to unsigned long long?
Matthew Flaschen
+1  A: 

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.

unwind
+2  A: 

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.

Matthew Flaschen