littlefs-fuse icon indicating copy to clipboard operation
littlefs-fuse copied to clipboard

Read zero bytes on newly truncated file

Open s-macke opened this issue 6 years ago • 0 comments

Following code creates a file and truncates it to 1MB. After that it tries to read the first 1MB. However, read command returns 0 bytes read, which is obviously wrong.

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

int main()
{
    char data[1024*1024];
    int fd = open("test", O_RDWR | O_CREAT | O_TRUNC, S_IRWXU);
    ftruncate(fd, sizeof data);
    lseek(fd, 0, SEEK_SET);
    int ret = read(fd, data, sizeof data);
    printf("bytes read: %i\n", ret);
    close(fd);
    return 0;
}

s-macke avatar Feb 04 '18 22:02 s-macke