0

After a long time to search a solution, I have to ask your precious help. I work on a program which implement "ls" unix command in C. I'd have only name of the file and his size. I looked that I've to use: "stat" and "dirent". I found a "solution" in Stackoverflow but didn't work perfectly for me. So I can show the names of the file into the directory but not their size. When I use gcc, whether it shows: 0 octet (while it isn't empty) or "

error: format ‘%s’ expects argument of type ‘char *’, but argument 3 has type ‘__off_t’ [-Werror=format=] printf("%s - %s", dp->d_name, s->st_size);

"

My test code (not clean):

#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>
#include <fcntl.h>
#include <errno.h>
#include <poll.h>

struct stat statbuf;
struct dirent *dp;
struct stat *s;

int main ()
{
DIR *dirp;
dirp = opendir("/tmp/gestrep");

while((dp = readdir(dirp)) !=NULL)
{

    stat(dp->d_name, &statbuf);
    printf("%s - %s", dp->d_name, s->st_size);
}

}

In fact, I don't know how to solve the format type problem. I saw I could use ftell/fseek but I don't have the right to use FILE* functions.

Thank you for all solutions :)

5
  • 1
    possible duplicate of Use printf to display off_t, nlink_t, size_t and others Commented Mar 4, 2015 at 18:47
  • Aren't you passing st_size (integral type) to a %s (null-terminated char string) format? Commented Mar 4, 2015 at 18:51
  • The first step in debugging is to read and understand the error. GCC is telling you exactly what and where the problem is. Commented Mar 4, 2015 at 18:52
  • When I change the %s to solve the format type problem with int size = s->st_size; and printf("%s - %d", dp->dp_name, size); It shows false informations about files Commented Mar 4, 2015 at 18:57
  • @user3723202 Instead of telling us that, you should show us a concrete example of a value that your program printed, and the value that was correct. s->st_size is normally not an int, so your value might be truncated when you did int size = s->st_size; if the file size is large. There are answers and pointers to similar question (like stackoverflow.com/questions/1401526/…) that shows you how to print out an off_t type. Commented Mar 7, 2017 at 12:06

1 Answer 1

1

You certainly cannot output the value of any integer type with a %s format code, and the error message you're getting from gcc should be perfectly clear.

Posix requires off_t to be an alias for some integer type, so a simple solution (with C11) would be to cast the value to an intmax_t (which is the widest integer type) and then use the j printf format size modifier:

printf("%s - %jd", dp->d_name, (intmax_t)s->st_size);

You'll need to make sure you include the appropriate header for intmax_t to be available:

#include <stdint.h>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.