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

what is that write function that you posted? It's not standard C or C++.

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.

Try itoa. It's not standard so there may be portability issues but I think most implementations have it.

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.

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

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.