Summarize and organize common library functions in C language in Linux-file operations and linux library functions

Source: Internet
Author: User

Summarize and organize common library functions in C language in Linux-file operations and linux library functions

When there is no IDE, it is necessary to remember the name, parameters, basic usage, and precautions of some common library functions.

Refer to the Linux_C_HS.chm directory. I roughly divide common functions into the following categories:

  • 1. Memory and string control and operations
  • 2. String Conversion
  • 3. Character Testing
  • 4. File Operations
  • 5. time and date
  • 6. Common mathematical functions
  • 7. File Content operations
  • 8. File Permission Control
  • 9. process operations
  • 10. Thread operations
  • 11. Socket operations
  • 12. Signal Processing
  • 13. data structures and algorithms

 

 

This article mainly summarizes the black section above, concerning file operation functions.

  • System Call category
** Function name usage remarks ** 1. int open (const char * pathname, int flags); open and possibly create a file or device flags must contain any one of O_RDONLY, O_WRONLY, or O_RDWR ** 2. int open (const char * pathname, int flags, mode_t mode); UP mode is valid only when flags contain O_CREAT. ** 3. int fsync (int fd); synchronize a file's in-core state with storage device must be synchronized before writing data close to avoid unexpected ** 4. off_t lseek (int fd, off_t offset, int whence); the third parameter for locating the file location can be Think SEEK_SET SEEK_CUR SEEK_END ** 5. ssize_t read (int fildes, void * buf, size_t nbyte); The UP file location will move as the number of bytes read ** 6. ssize_t write (int fildes, const void * buf, size_t nbyte); UP ** 7. int close (int fd); UP ** 8. void * mmap (void * addr, size_t length, int prot, int flags, memory ing first use fstat to get the file size, and then use this function to map the file content to the memory, then int fd, off_t offset); directly call the string function operation. ** 9. int munmap (void * addr, size_t length); release memory UP ** 10. int ftruncate (int fd, off_t length); truncate a file to a specified length when creating a file, if memory ing is used, you must first call this function to set a size for the file, otherwise, an error occurs ** 11. int fstat (int fd, struct stat * buf); for basic information about the file, see man. You can check the file size and modification date *

 

  • C99 Standard Classification
/* Standard C function * function name usage remarks ** 1. FILE * fopen (const char * path, const char * mode); NT mode can be r, r +, w, w +, a, a +, rw, etc. ** 2. int fseek (FILE * stream, long offset, int whence); NT locates the FILE location ** 3. long ftell (FILE * stream); current FILE location NT ** 4. void rewind (FILE * stream); move the FILE location to the FILE header NT ** 5. size_t fread (void * ptr, size_t size, size_t nmemb, FILE * stream); NT ** 6. size_t fwrite (const void * ptr, size_t size, size_t nmemb, FILE * stream); NT ** 7. int feof (FILE * stream); judge whether the FILE has been read NT ** 8. int fscanf (FILE * stream, const char * format ,...); reading file streams by format also has other functions in the scanf family. For example, sscanf can read byte streams ** 9. int fprintf (FILE * stream, const char * format ,...); write content to files in a format, such as sprintf and vprintf ** 10. int vfprintf (FILE * stream, const char * format, va_list ap); write the variable parameters directly to the FILE and the corresponding read letter ** 11. int fflush (FILE * stream); NT ** 12. char * fgets (char * s, int size, FILE * stream); NT stops reading line breaks or EOF ** 13. int fileno (FILE * stream); returns the integer FILE descriptor, which is the same as GLIBC ** 14. int fputs (const char * s, FILE * stream); NT does not write the terminator \ 0 **/

 

  • Summary of File Permission control functions
* Function name usage remarks ** 1. int remove (const char * pathname); NT delete the file in the specified path ** 2. int rename (const char * old, const char * new); NT rename ** 3. int utime (const char * path, const struct utimbuf * times); NT modifies the Object Access time ** 4. int stat (const char * restrict path, struct stat * restrict buf); NT can further obtain file DETAILS ** 5. int readdir_r (DIR * dirp, struct dirent * entry, struct dirent ** result); NT reads the folder content ** 6. DIR * opendir (const char * name); NT open the folder, this is the first step ** 7. char * getcwd (char * buf, size_t size); NT obtains the current absolute path ** 8. int chmod (const char * path, mode_t mode); NT changes the File Permission ** 9. int closedir (DIR * dirp); NT must be disabled ** 10 after opening the folder. int chdir (const char * path); NT changes the current working directory *

 

  • Additional functions used to process variable parameters

 

** 1. void va_start (va_list ap, last); must be called first ** 2. type va_arg (va_list ap, type); then, get the actual value ** 3 based on % s % d % c. void va_end (va_list ap); Call end after obtaining the required value. **

 

The above is just a simple summary of commonly used functions. Some less commonly used functions, such as links, are not classified.

 

These functions should at least be known to have these functions, but do not need to be implemented by yourself. The specific parameters are unclear. After knowing the function name, in Linux, man fun can be used for more details.

 


C language library function for copying files to other directories in linux

You can use the system function to execute shell command copy.
For example, system ("cp/home/file1/root/file2 ");
In addition, functions such as fread and fwrite mentioned above may fail because the file is too large to expand in the memory.

C language library function directory in linux

I already spoke about it. I will give it to you again.
The memset function of the kernel is used by the kernel itself. You cannot see this memset at the user level.
All functions in the kernel are implemented internally and no other libraries are used.
Memset is generally provided by glibc when you write an application,
At least make it clear that the kernel is completely self-sufficient without using other C libraries. The C-library functions you usually use cannot be found in the kernel.

I already spoke about it. I am giving it to you.
The memset function of the kernel is used by the kernel itself. You cannot see this memset at the user level.
All functions in the kernel are implemented internally and no other libraries are used.
Memset is generally provided by glibc when you write an application,
At least make it clear that the kernel is completely self-sufficient without using other C libraries. The C-library functions you usually use cannot be found in the kernel.

The kernel source code is not only used on the PC, but also available for other architectures. The arch folder contains the architecture-related code.

PC is generally x86

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.