Reputation: 261
So, I have an assignment that basically is an exercise in comparing the speed of system calls opposed to library functions. We're sorting a string we grab from a file through stdin. We're supposed to determine if the file is a regular file or not using fstat. I've read through the manual page and while I know WHAT it does, I'm having problem figuring out how to use it.
I know it returns a stat struct, so can I literally just make a variable and store it like that? Ex:
int n;
struct stat *val;
n = fstat(0, val);
Is that how you get the struct? Is it returned somewhere else? I need access to the off_t st_size variable so I know how many bytes the file is. And can that be cast to an int?
Also, apparently you can use the st_mode field to check if the file is regular (using the macros S_ISREG), but...how? Does it return true of false or something? It's annoying because I can find all these documents telling me what the fields are, but not how to use them.
If I have a regular file, I'm supposed to allocate enough memory to store it before any function calls. If not, then I reallocate memory as I read. I've got the second part done, I just have no idea how to use fstat correctly.
Upvotes: 1
Views: 1498
Reputation: 35881
fstat
doesn't allocate memory, you need to give it the address of pre-allocated space. For example:
int n;
struct stat myStat;
n = fstat(0, &myStat);
Note the lack of pointer.
Upvotes: 2