littlefs-fuse
littlefs-fuse copied to clipboard
Read zero bytes on newly truncated file
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;
}