c - What is the relation between fopen and open? -
i working on project class , given .c file containing following code:
int fd = -1; if (fd < 0) { fd = open ("my_dev", o_rdwr); if (fd < 0) { perror ("open"); return -1; } ...
so understand trying open file "my_dev" read/write permissions, , returning file descriptor on success or negative value on failure, dont understand why giving me "permission denied" consistently. tried use code:
int des = open("my_dev", o_creat | o_rdwr, 0777); ... close(des)
to open/create file (this called before other block), not work, , yet if use instead:
file* file = fopen("my_dev","w+"); fprintf(file,str); fclose(file);
i can write file, meaning have write permissions. normally, use fopen , fprintf everything, project, sort of have use teacher's .c file going try use
open()
which going give "permission denied" error in turn going screw code.
i guess question how fopen , open relate each other? seems able recite open system call whereas fopen standard lib function, cant seem find clear answer how can create file fopen() can opened open() without "permission denied" error, or how can create file open() can write to, close , open again open().
in short, how create file in c can write , open later open(o_rdwr)?
sorry if little piecey, im super tired.
ps: should noted compiling , running on university computer, permissions may "weird" should noted if create file terminal command "dd" open() work, , furthermore, have write permissions since can indeed write file fopen , fprintf
fopen
library function provided standard c runtime, returns stream , can call stream functions on it, fscanf
, fprintf
, or fread
, fwrite
.
open
system call on unix-like systems, provided operating system kernel, returns file descriptor, can call io functions fd, read
, write
.
generally fopen
implemented using open
underline.
if want use standard stream functions on file descriptor, can use posix api, fdopen
, takes fd, , returns file*
stream.
Comments
Post a Comment