Ok so I am having a problem using the method stat(), I need to find the filesize of a file and return it as a const char *, So i would need to convert the int value to a char. This is what i have now which does compile but it doesnt do anything now.

char pname[128]; // is equal to ./pages/index.html
int r = 0;
char tmp[20];
		r = stat(pname, &statbuf);
		sprintf (tmp, "%d",r);
	 	write(1,tmp,strlen(tmp));

T is a Linux xterm terminal that i use for compiling, I know it works but it takes const char * so ....

If you are attempting to display the file size then you are formatting the string with the wrong value. stat() doesn't return the file size, but it returns 0 if the file's stats were successfully retrieved, or non-zero on error. The file size is contained in struct _stat.

Ok so how would you propose to get it someone said do

struct stat results;
		stat(pname, &results);
		write(1,results.st_size,strlen(results.st_size));

But its having problems compile because i need it as a const char * for first argument and for the 2nd argument i need to use strlen() which also uses const char *

Will type casts work?
i.e. [b](const char *)[/b]results.st_size This should enable it to compile, but honestly, I suspect you'll get invalid results.

Edited 6 Years Ago by Fbody: n/a

Will type casts work?
i.e. [b](const char *)[/b]results.st_size This should enable it to compile, but honestly, I suspect you'll get invalid results.

Of course typecase will NOT work. You can not typecast an int into char* because it has to be converted. Very similar to the original post but use results.sz_size instead of the result value from stat() function.

struct stat results;
char buf[40];
stat(pname, &results);
sprintf(buf,"%d", results.st_size);
write(1,buf, strlen(buf));
warning: format %d expects type int, but argument 3 has type __off_t

This is the error message on using that solution from Ancient Dragon, Regardless of this error message it works perfectly just if you have any suggestions on fixing the error message.

Edited 6 Years Ago by Dizzzy: n/a

change %d go %u (unsigned int) or typecast the size into an int, which could produce unexpected results if the file size is larger than an int can hold.

warning: format %d expects type int, but argument 3 has type __off_t

Change %d to %ld . That'll most probably fix it, %ld tells that the argument will be long int . If the warning persists, post back.

[EDIT]
Badly beaten by AD it seems, though I'm still in favour of trying %ld

Edited 6 Years Ago by mitrmkar: n/a

Change %d to %ld . That'll most probably fix it, %ld tells that the argument will be long int . If the warning persists, post back.

[EDIT]
Badly beaten by AD it seems, though I'm still in favour of trying %ld

Is there a code for a long long int ? I've noticed that on some 32-bit systems a long is the same as an int.

Is there a code for a long long int ?

C99 standard says %lld and %I64d accepted at least by Microsoft and MingW/GCC.

This article has been dead for over six months. Start a new discussion instead.