I had to do a mini project where I had to use write() in C the code works fine but when I use valgrind to check it I get two errors
First:The first error I get in valgrind
and second:The second error I get
here is the function where I used write()
int add_to_file(dir_data_t *elem,char *output,char *name){
char path[1024];
snprintf(path, sizeof(path), "%s/%s", output, name);
int fd = open(path, O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR);
if (fd == -1) {
printf("Error opening file (add_to_file)n");
exit(EXIT_FAILURE);
}
ssize_t bytes_written;
bytes_written = write(fd, elem, sizeof(dir_data_t));
if (bytes_written == -1) {
perror("Error writing to the file (add_to_file)");
close(fd);
return 1;
}
close(fd);
return 0;
}
As I said the code works and I get everything done as I need but I am curious why do I get this and how to solve this eventually to not appear anymore? I use the structure as static so I don’t alloc memory in my code that’s why I don’t really understand how I got that.
Paradox is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.