简体   繁体   中英

Trying to open a file c system calls

I'm trying to open a binary file using system calls, I cannot use functions (fopen etc)

if ((fd2 = open("RandomStruct.bin",O_RDWR|O_CREAT)) == -1) 
        err_sys("Failed to open binary\n");

Is this line of code incorrect? thanks! btw I get ": Error 0" Oo

No, it is not correct. If you specify the O_CREAT flag, then open() needs another argument, which is the permission bits for the new file. eg

 open("RandomStruct.bin",O_RDWR|O_CREAT, 0755);

You need to add the file permissions as mentioned in the other answer, however, open() is not a system call it's just another function (or a wrapper), just like fopen() , in libc which in turn call the open system call. I think you should check syscall() instead, assuming you're using Linux, which allows you to invoke a system call directly by passing it's number (in this case SYS_open ) and arguments (if your assignment requires using system calls).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM