简体   繁体   中英

fstat getting file size zero

File is there and having json data inside it. I want to know length of file.but when i try below code it but size remains 0.

int file_contentl_Len = 0;
int fd_0 ;

fd_0 = open(FILE_PATH_CONFIG_0, O_WRONLY | O_TRUNC | O_CREAT, 0644);

if(fd_0 < 0)
{
    printf("\r\nError opening Config file %s: %s\n",FILE_PATH_CONFIG_0, strerror(errno));
    return -1;
}

struct stat buf;
fstat(fd_0, &buf);
file_contentl_Len = buf.st_size;


printf("\r\nConfig file %s content length: %d\r\n", FILE_PATH_CONFIG_0, file_contentl_Len);

You opened the file for writing with truncation, creating it if necessary — O_WRONLY | O_TRUNC | O_CREAT O_WRONLY | O_TRUNC | O_CREAT O_WRONLY | O_TRUNC | O_CREAT .

The size of zero tells you the truncation worked, or the file was created empty.

If you wanted to read what was in the file, use O_RDONLY instead. Or use O_RDWR and think carefully about whether to allow the file to be created.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM