Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.
#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.

transaction.txt properties

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.

share|improve this question
1  
This works (except fd= needs to be FILE* fd=) –  dv3500ea Apr 26 '11 at 16:31
    
This may be better asked on Stack Overflow or Code Review. You probably don't want to use sprintf or even a temporary variable. A couple of quick suggestions for functions to consider: fprintf(), fopen(), fclose(). –  jwernerny Apr 26 '11 at 17:04
    
@dv3500ea: FILE* is required by fopen, raw open return and int –  enzotib Apr 26 '11 at 19:09
    
@enzotib so that's why it was giving me a typecasting error. My C knowledge is lacking to say the least. So fd= needs to be int fd=. –  dv3500ea Apr 26 '11 at 20:19
    
I've diagnosed the problem although I don't know the solution. 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 human readable if you run sudo cat transaction.txt which gives ` 13` as expected. The real question here is how to fix the permissions of the file. –  dv3500ea Apr 26 '11 at 20:25
add comment

1 Answer

up vote 0 down vote accepted

Add the line

int fd;

and modify the open call as follows:

fd = open("transaction.txt", O_CREAT | O_WRONLY, 00644);
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.