Linux file operations, directory structure, file model manipulation functions

File operations
this chapter to achieve the goals:
O1. Understand the directory structure of the Linux file system;
o2. Understanding the Linux file model;
o3. Linux master function on the function of file operations;
O4. Linux system skilled application function performs the file operation programming.
11.1 Linux file system
o almost all file systems in the operating system used for
the user's structure is similar, have adopted tree or forest
structure of a forest
o is the root directory "/" In the UNIX family systems
o Linux there is no concept of letter, initial file path to
the root directory "/." Typically comprises in the root directory as follows
Catalog:
n-Boot start the associated program and configuration;
n-bin common Linux commands, which typically may be
; link execution file or of these files
command n sbin typically prepared for root user;
n- lib system commonly used libraries;
n-usr user installation files, libraries, the development of libraries;
n-the root root user file;
n-Home common user profile is;
n-profile etc system or program;
n-var system server data, ; logs
n proc system status information;
n-system device dev;
n-mnt, media mount point for other partitions (e.g.
Windows partitions, CD-ROM or floppy disk, etc.);
the n-tmp temporary files;
the n-Lost + found disk isolated sectors.
o For application development, we need to file as
a "flow."
o During the operation file, we need to control a fake
pointer in thought. The pointer to a location of the file,
this position is the position of the current operation file. This means the
pin will change as we move operation automatically
11.2 file operation related functions
o file control
o Directory Operations
o stream file read and write control
o the file read and write operations
11.2.1 file control
o rename the file
n #include <stdio.h >
n-int the rename (oldpath const char *,
const char * newpath is);
o delete the file
n-#include <stdio.h>
n-int remove (const char * pathname);
owner o modify file
n #include <sys / types .h>
n-#include <unistd.h>
n-int chown (const char * path, the uid_t
owner, gid_t, Group);
o modify access the file.
#include n-<SYS / types.h>
n-#include <SYS / defined in stat.h>
n-int the chmod (const char * path, mode_t
MODE);
O in addition it is also possible at a macro or in combination:
n-S_IRUSR / all file S_IREAD who has read permission,
the n-S_IWUSR / S_IWRITE have write access to the file owner,
the n-S_IXUSR / S_IEXEC have execute permissions for the file owner,
the n-S_IRGRP user group has read access,
the n-S_IWGRP user has write permissions group,
the n-S_IXGRP user group has execute permission ,
the n-S_IROTH all other users have read access,
the n-S_IWOTH all other users have write access,
the n-S_IXOTH all other users have execute permissions.
Directory Operations 11.2.2
o obtain the current working directory
n-#include <unistd.h>
n-char * getcwd (char * buf, size_t
size);
o change the current working directory
n-#include <unistd.h>
n-char * getcwd (const * path char);
O open directory
#include n-<SYS / types.h>
n-#include <dirent.h>
n-the opendir the DIR * (const char * name);
O close an open directory
n-#include <SYS / types.h>
n-#include <dirent .h>
n-int closedir (the DIR * the dir);
O read the contents of a file directory, and the directory stream refers
after shifting
n-#include <SYS / types.h>
n-#include <dirent.h>
n-struct dirent * the readdir (the DIR * the dir);
n-struct dirent {
n-ino_t the d_ino;
n-ff_t d_ff;
n-Signed Short int d_reclen is;
n-unsigned char d_type;
n-char d_name [256];
n-};
O gets the specified directory stream current pointer position
n #include <SYS / types.h>
n #include <dirent.h>
n-of off_t the telldir (the DIR * the dir);
O provided the specified directory stream needle position
n #include <sys/types.h>
#include n-<dirent.h>
n-seekdir void (the dir the DIR *, of off_t offset);
11.2.3 write control file stream
o open file, get file stream pointer
n-#include <stdio.h>
n-the FILE * the fopen (const * path char, const
char * MODE);
O close an open file
n #include <stdio.h>
n-int fclose (the fILE * stream);
O acquiring file stream read pointer to the current position of the
n #include <stdio.h >
n-Long ftell (the fILE * stream);
O setting file stream read pointer position
n-#include <stdio.h>
n-int fseek (the fILE * stream, Long offset,
int the whence);
O Analyzing current read pointer file stream whether the write has reached the position of
end of file
n-#include <stdio.h>
n-int feof (the fILE * stream);
O reads a character from the specified file stream
n-#include <stdio.h>
n-int fgetc (the fILE * stream );
o The single character written to the specified file stream
n #include <stdio.h>
n-int of fputc (C int, the FILE * Stream);
o Read a string from the specified file stream
n #include <stdio.h >
n-char * fgets (S char *, int size, the fILE
* stream);
O will be written to the specified file stream string
n-#include <stdio.h>
n-int fputs (char * S, the fILE * stream);
o a piece of data read from the specified file stream
n-#include <stdio.h>
n-size_t fread (PTR void *, size_t size,
size_t nmenb, the fILE * stream);
o period specified data into the specified file stream
n # the include <stdio.h>
n-size_t fwrite (void * PTR, size_t size,
size_t nmenb, the fILE * Stream);
11.2.4 file read and write operations
o opens the specified file and return a file identifier
n #include <sys / types.h >
n-#include <SYS / defined in stat.h>
n-#include <the fcntl.h>
Open int n-(pathname const char *, int
the flags, MODE mode_t);
n-of O_RDONLY open file in read-only form,
n-O_WRONLY opens the file in write-only form,
n-O_RDWR open the file in read-write form;
n-if the O_CREAT the open file does not exist, the file automatically created,
n-the O_EXCL use with the O_CREAT, if the file exists error;
when n O_TRUNC be opened in write mode, empty the contents of the file.
o Create a new file
n-#include <SYS / types.h>
n-#include <SYS / defined in stat.h>
n-#include <fcntl.h>
n-int creat (const char * pathname,
mode_t MODE);
o Create temporary memory file
#include n-<stdlib.h>
n-mktemp is int (char * Template);
O Close file
n-#include <unistd.h>
n-int Close (int FD);
O read from the specified file
n #include <unistd .h>
n-Read an ssize_t (FD int, void * buf, size_t
COUNT);
o data into the specified file
n-#include <unistd.h>
n-Write an ssize_t (FD int, void * buf, size_t
COUNT);
o modify the file read-write position
n-#include <SYS / types.h>
n- #include <unistd.h>
n-of off_t lseek (int FD, of off_t offset, int
the whence);
O unnamed conduit create
n-#include <unistd.h>
n-int pipe (int the filedes [2]);
O creates the named pipe, the document is regarded as a duct
n-#include <SYS / types.h>
n-#include <SYS / defined in stat.h>
n-int the mkfifo (const char * pathname,
mode_t MODE);
11.3 application example training
examples training o chapter, we to complete a "little speech
code", which is a simplified version of an electronic dictionary software.
We mainly borrowed the development of the software to understand the Linux
file operations. As a simplified version of the electronic dictionary, I
have to provide the following two functions: look up the words, add
add the word.
11.3.1 Program Analysis
o For the data in the file organization, often classified into two categories,
one is to provide data for the computer is running, such documents often stored in binary form line, the class file humans
via text viewer software can not read, but because which
storage format with the internal operation of a computer data format basically
the same, so it was easier to read a computer program;
the other is to read or modify human provided, such text
element is often used for storing text, although these files
may be read through the text view software, but for the meter
computer reading too much trouble, often need to convert.
o After selecting the text stored in the form, use the flow way
more convenient, so here we choose the standard library functions
provide correlation function file stream operation
level o software should be. First, we need to provide
for a package to read and write functions. Secondly, again on top of providing
dictionary manipulation functions. Written in the uppermost complete interface.
11.3.2 write program
o / * function definition * /
O void FileReadLine (File the FILE *, char * buf);
O void FileWriteLine (File the FILE *, char * buf);
O
O int DictFindWord (File the FILE *, char * Word, char
* exp);
O void DictAddWord (the FILE * File, Word char *, char
* exp);
o
o int DecodeCommand(char *cmdbuf, char
*wordbuf, char *expbuf, int bufsize);
o int GetCommandCatagory(char cmdcatach);
o void PrintHelpInfo();
11.3.3  编译与运行
o gcc dictionary.c -o dictionary
o ./dictionary
 

Guess you like

Origin blog.csdn.net/qq_38971487/article/details/91492753
Recommended