Professional Documents
Culture Documents
Reference
1. Unix System Programming, Keith Haviland, Dina Gray and Ben Salama, Addison-Wesley
UNIX primitives
Name
open creat
Meaning
Opens a file for reading or writing or creates an empty file Creates an empty file
close
read write lseek unlink remove fcntl
#include <fcntl.h> #include <unistd.h> int main(void) { int fd; ssize_t nread; char buf[1024]; /*open file for reading */ fd = open("marks.dat", O_RDONLY); /* read the data */ nread = read(fd, buf, 1024); /*close the file */ close(fd);
Value of a pathname can be absolute pathname, such as /home/sanjay/sample.dat It can be relative pathname which is in relation to pwd. flags: of integer type, specifies the access method, constants defined in <fcnlt.h> (file control)
Sample programs
createfile.c readfile.c readfileeof.c readfileeof02.c copyfile01.c createmarksfile.c appendfile.c printmarksst.c
#include <unistd.h> int main(void) { int fd; ssize_t nread; char buf[1024]; char *filename="marks.dat"; /*open file for reading */ fd = open(filename, O_RDONLY); if (fd == -1) { printf("Coundn't open %s\n", filename); exit(1); } /* read the data */ nread = read(fd, buf, 1024); /*Processing on data*/ /*close the file */ close(fd);
exit(0);
}
What happens if filename already exists? If access permissions allow, file will be opened for writing as if O_CREAT was not specified. Use O_CREAT flag with O_EXCL flag, which will cause an open to fail, if the file already exists. fd = open(filename, O_WRONLY | O_CREAT | O_EXCL, 0644);
All open files are automatically closed when a program completes execution.
You can replace 1024 by variable like SOMEVALUE nread gives the number of characters actually read Like open, if something goes wrong, read will return -1.
What if a program opens an existing file for writing and then immediately write to that file? Old data will be overwritten File can be opend with O_APPEND flag.
fd = open(filename, O_WRONLY | O_APPEND);
Second parameter, offset, actually determines the new position of the read-write pointer. It gives number of bytes to add to a starting position. The starting position will be determined by the third argument, the integer start_flag. It specifies where in the file the offset is to be measured from.
SEEk_SET
A B C
D
E F
SEEk_CUR
G
h
SEEk_END
Use of O_APPEND
filedes=open(filename, O_WRONLY | O_APPEND); write(filename, appbuf, BUFSIZE);
Deleting a file
#include <unistd.h> #int unlink(const char *pathname); #include <stdio.h> int remove(const char *pathname); unlink(filename); remove(filename);
It acts on the open file identified by the file descriptor Programmer selects a particular function by choosing a value for the integer cmd parameter from the header file <fcntl.h>
Symbolic mode
S_IRUSR S_IWUSR S_IXUSR S_IRGRP S_IWGRP S_IXGRP S_IROTH S_IWOTH
Meaning
Read allowed by owner Write allowed by owner Owner can execute file Read allowed by group Write allowed by group Group member can execute file Other types of user can read file Other types of user can write file
0001
S_IXOTH
0400 S_ISUID Set user-id on execution 0200 S_ISGID Set group-id on execution 0100 S_ISVTX Save-text-image (sticky bit)
amode contains a value indicating the required method of access: R_OK Has calling process read access? W_OK Has calling process write access? X_OK Can calling process execute the file?