1. Trang chủ
  2. » Công Nghệ Thông Tin

Lập trình Linux IO pps

45 526 4

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 45
Dung lượng 167 KB

Nội dung

TMA Training Center (www.ttc.edu.com) Slide # 1 Linux I/O Programming TMA Training Center (TTC) TMA Training Center (TTC) TMA Training Center (www.ttc.edu.com) Slide # 2 1. Objective 2. Unix file system 3. Standard I/O Library - Opening file - Closing file - Reading file - Writing file - Seeking file 4. Check user's permissions for a file 5. Get file’s status 6. Change permissions of a file 7. Reading The Contents Of Directories Contents TMA Training Center (www.ttc.edu.com) Slide # 3  The following tutorial describes various common methods for reading and writing files and directories on a Unix system.  Part of the information is common C knowledge, and is repeated here for completeness Objectives TMA Training Center (www.ttc.edu.com) Slide # 4  Files are stored in the filesystem in two pieces:  (1) a chunk of data somewhere in the filesystem;  (2) a data structure which contains: location, size, creation/modification/access times, ownership, access attributes of and links to the file. This data structure is called an "inode.“ The only thing about the file not included in the inode is the name of the file Unix File System TMA Training Center (www.ttc.edu.com) Slide # 5 Unix INODE Structure TMA Training Center (www.ttc.edu.com) Slide # 6  One of the unique things about Unix as an operating system is that regards everything as a file.  Files can be divided into four categories  Ordinary or plain files  Directories  Special or device files.  Links Everything is a File TMA Training Center (www.ttc.edu.com) Slide # 7  / - Special file system that incorporates the files under several directories including /dev, /sbin, /tmp etc  /usr - Stores application programs  /var - Stores log files, mails and other data  /tmp - Stores temporary files A few of the file system TMA Training Center (www.ttc.edu.com) Slide # 8  The FILE structure is the basic data type used when handling files with the standard C library.  When we open a file, we get a pointer to such a structure, that we later use with all other operations on the file, until we close it. Standard "C" File Read And Write - FILE Structure TMA Training Center (www.ttc.edu.com) Slide # 9 In order to work with a file, we must open it first, using the fopen() function. include <stdio.h> FILE *fopen(const char *path, const char *mode); The argument mode points to a string beginning with one of the following sequences r Open text file for reading. r+ Open for reading and writing. w Truncate file to zero length or create text file for writing. w+ Open for reading and writing. a Open for appending (writing at end of file). a+ Open for reading and appending (writing at end of file). Standard "C" File Read And Write - Opening a File (1/3) TMA Training Center (www.ttc.edu.com) Slide # 10 Open the file /home/choo/data.txt for reading FILE* f_read; f_read = fopen("/home/choo/data.txt", "r"); if (!f_read) { /* open operation failed. */ perror("Failed opening file '/home/choo/data.txt' for reading:"); exit(1); } Open the file logfile in the current directory for writing. /* if the file does not exist, it is being created. if the file already exists, its contents is erased. */ FILE* f_write; f_write = fopen("logfile", "w"); Standard "C" File Read And Write - Opening A File (2/3) [...]... ftell() #include long ftell(FILE *stream); The ftell function obtains the current value of the file position indicator for the stream pointed to by stream Return value Upon successful completion, ftell returns the current offset Otherwise, -1 is returned and the global variable errno is set to indicate the error Example long old_position = ftell(f_readwrite); if (old_position < 0) { perror("ftell");... And Write - Moving The Read/Write Location In An Open File – fseek() (1/2)  #include int fseek(FILE *stream, long int offset, int whence); The fseek() function allows us to move the read/write pointer of a file stream to a desired location, stated as the number of bytes from the beginning of the file (or from the end of file, or from the current position of the read/write pointer) TMA Training... close operation  Whether the function succeeded or not, the FILE structure may not be used any more by the program TMA Training Center (www.ttc.edu.com) Slide # 15 Standard "C" File Read And Write - Reading From An Open File – fgetc() Once we have a pointer for an open file's structure, we may read from it using any of several functions #include int fgetc(FILE *stream); The fgetc function returns... Calls - check user's permissions for a file (3/4) Check if we may execute the program file "runme" if (access("runme", X_OK) == 0) printf("Execute permission to program 'runme' granted.\n"); else printf("Execute permission to program 'runme' denied.\n"); Check if we may write new files to directory "/etc/config" if (access("/etc/config", W_OK) == 0) printf("File creation permission to directory '/etc/sysconfig'... Calls - Get file’s status (1/4)   Note that we cannot use access() to check why we got permissions (i.e if it was due to the given mode granted to us as the owner of the file, or due to its group permissions or its word permissions) For more fine-grained permission tests, see the stat() system call mentioned below #include #include #include int stat(const char... 22 Accessing Files With System Calls - check user's permissions for a file (1/4)  Since Unix supports access permissions for files, we would sometimes need to check these permissions, and perhaps also manipulate them  Two system calls are used in this context, access() and chmod()  The access() system call is for checking access permissions to a file #include int access(const char *pathname,... "C" File Read And Write - Moving The Read/Write Location In An Open File – fseek() (2/2) /* move the read/write pointer of the file stream to position '30' in the file Note that the first position in the file is '0', not '1' */ fseek(f_read, 29L, SEEK_START); /* move the read/write pointer of the file stream 25 characters forward from its given location */ fseek(f_read, 25L, SEEK_SET); TMA Training Center... '/etc/sysconfig' granted.\n"); else printf("File creation permission to directory '/etc/sysconfig' denied.\n"); TMA Training Center (www.ttc.edu.com) Slide # 25 Accessing Files With System Calls - check user's permissions for a file (4/4) Check if we may read the contents of directory "/etc/config" if (access("/etc/config", R_OK) == 0) printf("File listing read permission to directory '/etc/sysconfig' granted.\n");... Training Center (www.ttc.edu.com) Slide # 18 Standard "C" File Read And Write - Writiing From An Open File – fputc() #include int fputc(int c, FILE *stream); Just like the read operations, we have write operations as well They are performed at the current location of the read/write pointer kept in the FILE structure int 'a'; char buf[201]; c = fputc(c, f_readwrite); strcpy(buf, "hello world");... set user ID on execution set group ID on execution sticky bit read by owner write by owner execute/search by owner read by group write by group execute/search by group read by others Slide # 31 Accessing Files With System Calls - Change permissions of a file – chmod() (2/2) S_IWOTH S_IXOTH 00002 write by others 00001 execute/search by others Give the owner read and write permission to the file "blabla", . # 19 #include <stdio.h> int fputc(int c, FILE *stream); Just like the read operations, we have write operations as well. They are performed at the current location of the read/write pointer. file's structure, we may read from it using any of several functions. #include <stdio.h> int fgetc(FILE *stream); The fgetc function returns the next byte, as a character, from a file stream of data somewhere in the filesystem;  (2) a data structure which contains: location, size, creation/modification/access times, ownership, access attributes of and links to the file. This data

Ngày đăng: 29/07/2014, 14:20

TỪ KHÓA LIÊN QUAN

w