#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <sys/stat.h>
void main(){
char tmp[12]={0x0};
int i=13;
sprintf(tmp,"%11d", i);
int fd = open("transaction.txt", O_CREAT | O_RDWR);
write(fd, tmp, sizeof(tmp));
}
I'm trying to write integers to a file.
because linux does not offer itoa function, I have to try to use sprintf.
but I can't read the result file.
The file created is owned by me but has permissions ------xr-x
. This means that my user can't read or write the file and my group has no access at all but strangely other users can read it.
The file is readable if I run sudo cat transaction.txt
which gives 13
as expected.
How can I generate a file with suitable permissions so that is is usable.
fd=
needs to beFILE* fd=
)FILE*
is required byfopen
, rawopen
return andint
fd=
needs to beint fd=
.------xr-x
. This means that my user can't read or write the file and my group has no access at all but strangely other users can read it. The file is human readable if you runsudo cat transaction.txt
which gives ` 13` as expected. The real question here is how to fix the permissions of the file.