Lập trình Linux IO
Số trang: 45
Loại file: ppt
Dung lượng: 167.00 KB
Lượt xem: 13
Lượt tải: 0
Xem trước 5 trang đầu tiên của tài liệu này:
Thông tin tài liệu:
Các hướng dẫn sau đây mô tả khác nhau phổ biếnphương pháp để đọc và ghi các tập tin và thư mụctrên một hệ thống Unix. Một phần của thông tin được phổ biến kiến thức C, vàđược lặp lại ở đây cho đầy đủ
Nội dung trích xuất từ tài liệu:
Lập trình Linux IO TMA Training Center (TTC) LinuxI/OProgrammingTMA Training Center (www.ttc.edu.com) Slide # 1Contents 1. Objective 2. Unix file system 3. Standard I/O Library - Opening file - Closing file - Reading file - Writing file - Seeking file 4. Check users permissions for a file 5. Get file’s status 6. Change permissions of a file 7. Reading The Contents Of DirectoriesTMA Training Center (www.ttc.edu.com) Slide # 2Objectives 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 completenessTMA Training Center (www.ttc.edu.com) Slide # 3UnixFileSystem 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 fileTMA Training Center (www.ttc.edu.com) Slide # 4UnixINODEStructureTMA Training Center (www.ttc.edu.com) Slide # 5EverythingisaFile 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 TMA Training Center (www.ttc.edu.com) Slide # 6Afewofthefilesystem / - 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 filesTMA Training Center (www.ttc.edu.com) Slide # 7StandardCFileReadAndWriteFILEStructure 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.TMA Training Center (www.ttc.edu.com) Slide # 8StandardCFileReadAndWriteOpeningaFile(1/3)In order to work with a file, we must open it first, using the fopen() function.include FILE *fopen(const char *path, const char *mode);The argument mode points to a string beginning with one of the following sequencesr 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).TMA Training Center (www.ttc.edu.com) Slide # 9StandardCFileReadAndWriteOpeningAFile(2/3)Open the file /home/choo/data.txt for readingFILE* 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);TMA Training Center (www.ttc.edu.com) Slide # 10StandardCFileReadAndWriteOpeningAFile(3/3) Open the file /usr/local/lib/db/users for both reading and writing /* Any data written to the file is written at the beginning of the file, over-writing the existing data. */ FILE* f_readwrite; f_readwrite = fopen(/usr/local/lib/db/users, r+); Open the file /var/adm/messages for appending. /* Any data written to the file is appended to its end. */ FILE* f_append; f_append = fopen(/var/adm/messages, a);TMA Training Center (www.ttc.edu.com) Slide # 11StandardCFileReadAndWriteerrno errno is a special system variable that is set if a system call cannot perform its set task. It is defined in #include . To use errno in a C program it must be declared via: extern int errno; It can be manually reset within a C program (although this is uncommon practice) otherwise it simply retains its last value returned by a system call or library functionTMA Training Center (www.ttc.edu.com) Slide # 12StandardCFileReadAndWriteperror() The function perror() is prototyped by: void perror(const char *message); perror() produces a message (on standard error output), describing the last error encountered, returned to errno (see below) during a call to a system or library function. The argument string message is printed first, then a colon and a blank, then the message and a newline. If message is a NULL pointer or points to a null string, the colon is not printed.TMA Training Center (www.ttc.edu.com) Slide # 13StandardCFileReadAndWrite–Closingafile–fclose()(1/2) Once we are done working with the file, we need to close it. This has two effects: - Flushing any un-saved changes to disk (actually, to the operating systems disk cache). - Freeing the file descriptor (will be explained in the system calls section below) and any ot ...
Nội dung trích xuất từ tài liệu:
Lập trình Linux IO TMA Training Center (TTC) LinuxI/OProgrammingTMA Training Center (www.ttc.edu.com) Slide # 1Contents 1. Objective 2. Unix file system 3. Standard I/O Library - Opening file - Closing file - Reading file - Writing file - Seeking file 4. Check users permissions for a file 5. Get file’s status 6. Change permissions of a file 7. Reading The Contents Of DirectoriesTMA Training Center (www.ttc.edu.com) Slide # 2Objectives 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 completenessTMA Training Center (www.ttc.edu.com) Slide # 3UnixFileSystem 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 fileTMA Training Center (www.ttc.edu.com) Slide # 4UnixINODEStructureTMA Training Center (www.ttc.edu.com) Slide # 5EverythingisaFile 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 TMA Training Center (www.ttc.edu.com) Slide # 6Afewofthefilesystem / - 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 filesTMA Training Center (www.ttc.edu.com) Slide # 7StandardCFileReadAndWriteFILEStructure 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.TMA Training Center (www.ttc.edu.com) Slide # 8StandardCFileReadAndWriteOpeningaFile(1/3)In order to work with a file, we must open it first, using the fopen() function.include FILE *fopen(const char *path, const char *mode);The argument mode points to a string beginning with one of the following sequencesr 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).TMA Training Center (www.ttc.edu.com) Slide # 9StandardCFileReadAndWriteOpeningAFile(2/3)Open the file /home/choo/data.txt for readingFILE* 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);TMA Training Center (www.ttc.edu.com) Slide # 10StandardCFileReadAndWriteOpeningAFile(3/3) Open the file /usr/local/lib/db/users for both reading and writing /* Any data written to the file is written at the beginning of the file, over-writing the existing data. */ FILE* f_readwrite; f_readwrite = fopen(/usr/local/lib/db/users, r+); Open the file /var/adm/messages for appending. /* Any data written to the file is appended to its end. */ FILE* f_append; f_append = fopen(/var/adm/messages, a);TMA Training Center (www.ttc.edu.com) Slide # 11StandardCFileReadAndWriteerrno errno is a special system variable that is set if a system call cannot perform its set task. It is defined in #include . To use errno in a C program it must be declared via: extern int errno; It can be manually reset within a C program (although this is uncommon practice) otherwise it simply retains its last value returned by a system call or library functionTMA Training Center (www.ttc.edu.com) Slide # 12StandardCFileReadAndWriteperror() The function perror() is prototyped by: void perror(const char *message); perror() produces a message (on standard error output), describing the last error encountered, returned to errno (see below) during a call to a system or library function. The argument string message is printed first, then a colon and a blank, then the message and a newline. If message is a NULL pointer or points to a null string, the colon is not printed.TMA Training Center (www.ttc.edu.com) Slide # 13StandardCFileReadAndWrite–Closingafile–fclose()(1/2) Once we are done working with the file, we need to close it. This has two effects: - Flushing any un-saved changes to disk (actually, to the operating systems disk cache). - Freeing the file descriptor (will be explained in the system calls section below) and any ot ...
Tìm kiếm theo từ khóa liên quan:
ngôn ngữ lập trình lập trình căn bản lập trình linux hệ thống linux tài liệu lập trình linuxGợi ý tài liệu liên quan:
-
Giáo trình Lập trình hướng đối tượng: Phần 2
154 trang 255 0 0 -
Kỹ thuật lập trình trên Visual Basic 2005
148 trang 245 0 0 -
Bài thuyết trình Ngôn ngữ lập trình: Hệ điều hành Window Mobile
30 trang 244 0 0 -
Giáo trình Lập trình cơ bản với C++: Phần 1
77 trang 228 0 0 -
114 trang 220 2 0
-
Bài giảng Một số hướng nghiên cứu và ứng dụng - Lê Thanh Hương
13 trang 204 0 0 -
Giáo án Tin học lớp 11 (Trọn bộ cả năm)
125 trang 197 1 0 -
80 trang 197 0 0
-
NGÂN HÀNG CÂU HỎI TRẮC NGHIỆM THIẾT KẾ WEB
8 trang 179 0 0 -
Giáo trình Lập trình C căn bản: Phần 1
64 trang 158 0 0