Linux/unix File manipulation functions

Source: Internet
Author: User
Tags fread lstat rewind sprintf symlink

This article is used to record common file manipulation functions in Linux programming, including system calls and library functions, for review.

One problem with direct use of the underlying system calls for input and output operations is that they are inefficient because:

1. Using system calls can affect the performance of the system, which is much more expensive than a function call because Linux must switch from running user code to executing kernel code and then returning user code when executing system calls.
2. Hardware restricts the size of data blocks that can be read and written to the underlying system call at a time. For example, a tape drive can write a block length of 10K at a time, so if the amount of data you are trying to write is not an integer multiple of 10K, the tape drive is still winding the tape in 10K, leaving a gap on the tape


#include <unistd.h>
size_t write (int fildes, const void *buf, size_t nbytes);
The system invokes the Write function: writes the first nbytes bytes of the buffer buf to a file associated with the file descriptor Fildes. It returns the number of bytes actually written, which may be less than nbytes if the file descriptor is wrong or the underlying device driver is sensitive to the block length. If the function returns 0, it means that no data is written, and if 1 is returned, an error occurs in the write call, and the error code is saved in the global variable errno.


#include <unistd.h>
size_t read (int fildes, void *buf, size_t nbytes);
The system calls the Read function: reads nbytes bytes of data from the file associated with the file descriptor Fildes and puts them in the data area buf. It returns the number of bytes actually read, which may be less than the number of bytes requested. If the function returns 0, it means that no data has been read and the end of the file has been reached. If 1 is returned, there is an error in the read call, and the error code is stored in the global variable errno.


#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
int open (const char *path, int oflags);
int open (const char *path, int oflags, mode_t mode);
The system calls the Open function: The call successfully returns a unique new file descriptor (always a non-negative integer), returns 1 on failure, and sets the global variable errno. Prepares the name of the file or device to be opened as the parameter path passed to the function, the Oflags parameter specifies the action taken to open the file. Oflags Optional parameters are as follows:


When you use an open call with the O_CREAT flag to create a file, you must use an open invocation with 3 parameter formats, where the third parameter mode has the following flag bit:

The above flag bits are defined in the header file Sys/stat.h


#include <unistd.h>
int close (int fildes);
Terminates the association between the file descriptor Fildes and its corresponding file. The file descriptor is freed and can be reused. The close call returns 0 when it succeeds, and returns 1 when an error occurs.


#include <unistd.h>
#include <sys/types.h>
off_t lseek (int fildes, off_t offset, int whence);
The offset parameter is used to specify the position, while the whence parameter defines the offset value, whence can take the following values:


Lseek returns the byte offset value from the file header to the set of the file pointer, returning 1 on failure.


#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
int fstat (int fildes, struct stat *buf);
int stat (const char *path, struct stat *buf);
int Lstat (const char *path, struct stat *buf);
The Fstat system call returns the status information for the file associated with the open file descriptor, which is written to a BUF structure, and the BUF address is passed as a parameter to the Fstat
The correlation functions stat and Lstat return the status information that is found through the file name. They produce the same results, but when the file is a symbolic link, Lstat returns information about the symbolic link itself, and Stat returns the information for the file that the link points to.
The stat structure generally consists of the following members:


The St_mode flags returned in the stat structure also have macros associated with them, which are defined in sys/stat.h.
These macros include definitions for access rights, file type flags, and some masks to help test specific types and permissions.



#include <unistd.h>
int dup (int fildes);
int dup2 (int fildes, int fildes2);
The DUP system call provides a way to copy a file descriptor so that we can access the same file by two or more different descriptors. This can be used to read and write data at different locations in the file. The DUP system calls the copy file descriptor Fildes and returns a new descriptor. The DUP2 system call is to copy one file descriptor to another by explicitly specifying the target descriptor.


#include <stdio.h>
FILE *fopen (const char *filename, const char *mode);
The fopen library function is similar to the underlying open system call. It is used primarily for file and terminal input and output. If you need explicit control over your device, it is best to use system calls, as this avoids some potential problems with library functions, such as input/output buffering.
Fopen opens the file specified by the filename parameter and associates it with a file stream. The mode parameter specifies how the file is opened, taking the values in the following string:


Fopen returns a non-null file* pointer on success, returns a null value on failure, and a null value defined in Stdio.h


#include <stdio.h>
size_t fread (void *ptr, size_t size, size_t nitems, FILE *stream);
The Fread library function is used to read data from a file stream. The data is read from the file stream stream into the data buffer pointed to by PTR. The size parameter specifies the length of each data record, and the counter Nitems gives the number of records to transfer. Its return value is the number of records (not bytes) that were successfully read into the data buffer. When the end of the file is reached, its return value can be less than nitems, or even zero.


#include <stdio.h>
size_t fwrite (const void *ptr, size_t size, size_t nitems, FILE *stream);
The Fwrite library function is used to fetch data records from the specified data buffers and write them to the output stream. The size parameter specifies the length of each data record, and the counter Nitems gives the number of records to write. Its return value is the number of records that were successfully written (not the number of bytes). When the end of the file is reached, its return value can be less than nitems, or even zero.


#include <stdio.h>
int fclose (FILE *stream);
The Fclose library function closes the specified 1 file stream stream so that all data that has not been written out is written out. Because the Stdio library buffers data, it is important to use fclose. When the program ends normally, the Fclose function is automatically called for all open file streams


#include <stdio.h>
int fflush (FILE *stream);
The purpose of the Fflush library function is to write out all the data in the file stream without writing it immediately. It is important to note that the Fclose function implicitly calls Fflush, so you do not have to call Fclose before calling Fflush.


#include <stdio.h>
int fseek (FILE *stream, long int offset, int whence);
The Fseek function is the file stream function that corresponds to the Lseek system call, which specifies the location for the next read and write operation in the file stream. The value of the offset and whence parameters is exactly the same as the Lseek system call, but Lseek returns a off_t value, and Fseek returns an integer, 0 for success, 1 for failure and set errno to indicate an error.


#include <stdio.h>
int fgetc (FILE *stream);
int getc (FILE *stream);
int GetChar ();
The FGETC function takes the next byte out of the file stream and returns it as a character. When it reaches the end of the file or an error occurs, it returns EOF. The two cases must be distinguished by the ferror or Feor function. The GETC function acts on fgetc, but it can be implemented as a macro. The GetChar function is equivalent to GETC (stdin), which reads a character from the standard input.


#include <stdio.h>
int FPUTC (int c, FILE *stream);
int PUTC (int c, FILE *stream);
int Putchar ();
These three function relationships are similar to the above three function relationships, Putchar equivalent to PUTC (c, stdout), which writes a single character to the standard output. It is important to note that both Putchar and GetChar use characters as int rather than char, which allows the end of file (EOF) to identify the value-1, which is a value that is beyond the range of the character's numeric encoding.


#include <stdio.h>
Char *fgets (char *s, int n, FILE *stream);
Char *gets (char *s);
The Fgets function reads a string from the input file stream stream into the string that s points to, until one of the following conditions occurs: A newline character has been transmitted, n-1 characters have been transferred, or the end of the file has been reached. It also passes the encountered newline character to the receiving string, plus a null byte to the end. A call can only transfer n-1 characters, because it must add an empty byte to the end string. When the call succeeds, a pointer to the string s is returned. If the file stream has reached the end of the file, Fgets sets the EOF ID for the file stream and returns a null pointer. If a read error occurs, FGETS returns a null pointer and sets errno to indicate the type of error.
The Get function is similar to fgets, but it reads data from the standard input and discards the line breaks encountered. It appends a nul byte to the end of the receiving string. It is important to note that the gets has no limit on the number of characters transmitted, so it may overflow its own transmit buffer.


#include <stdio.h>
int fputs (const char * s, FILE * stream);
int puts (const char *string);
The Fputs function writes the string referred to in the argument s to the output file stream stream, and returns the number of characters written if the call succeeds, or if the return EOF indicates that a write error occurred.
The puts function is similar to fputs, which outputs the string that the parameter s refers to in standard output. It does not stop the output until it encounters a null byte. and automatically outputs a newline character when the string is finished. \ n


#include <stdio.h>
int printf (const char * format, ...);
int sprintf (char *s, const char * format, ...);
int fprintf (FILE *stream, const char * format, ...);
The printf function sends its own output to the standard output. The fprintf function sends its own output to a specified file stream. The sprintf function writes its own output and a trailing null character to the string s passed in as a parameter. This string must be sufficient to accommodate all the output data.


#include <stdio.h>
int scanf (const char * format, ...);
int sscanf (const char *s, const char * format, ...);
int fscanf (FILE *stream, const char * format, ...);
The values read into the scanf function are saved to the corresponding variables, and they must be of the correct type, and they have to match the format string exactly. Otherwise, the memory data may be compromised, causing the program to crash. The format format string for the SCANF series function contains both ordinary characters and conversion controls, which are used to specify the characters that must appear in the input data.
For example scanf ("Hello%d", &num);
The scanf call succeeds only if the next five characters in the standard input match "Hello".
The SCANF function 1 returns a value that is the number of data items it successfully reads, and if the first data item fails, its return value is zero. If the end of the input is reached before the first data item is matched, it returns EOF. If the file stream sends a read error, the stream error flag is set and the error variable errno is set to indicate the type of error.


#include <errno.h>
extern int errno;
Many functions can change the value of errno, whose value is only meaningful if the function call fails. It must be checked immediately after the function indicates a failure. It should be copied to another variable before use, because an output function like fprintf can change the value of errno, or it can check the state of the file stream to determine if an error has occurred or if it has reached the end of the file. The values and meanings of the error codes are listed in the header file Errno.h, which generally include:

#include <string.h>
char *strerror (int errnum);
The Strerror function maps the error code to a string that describes the type of error that occurred.


#include <stdio.h>
void perror (const char *s);
The Perror function also maps the current error reported in the errno variable to a string and outputs it to the standard error output stream. Precede the string with the information given in the string s (if not empty), plus a colon and a space.


#include <stdio.h>
int ferror (FILE *stream);
int feof (FILE *stream);
void Clearerr (FILE *stream);
The FERROR function tests the error ID of a file stream and returns 0 if the identity is set to return a value other than 0.
The feof function tests the end-of-file identification of a file stream and returns 0 if the identity is set to return a value other than 0.
The role of the Clearerr function is to clear the end-of-file identification and error identification of the file stream that is pointed to by stream. It does not have a return value and does not define any errors.


#include <stdio.h>
int Fileno (FILE *stream);
FILE *fdopen (int fildes, const char *mode);
You can determine which underlying file operator the file stream is using by calling the Fileno function. It returns the file descriptor used by the specified file stream and returns 1 if it fails.
You can create a new file stream on an open file descriptor by calling the Fdopen function, which essentially provides a stdio buffer for an already opened file descriptor.


#include <sys/stat.h>
int chmod (const char *path, mode_t mode);
chmod system calls are used to change the access rights of a file or directory. The parameter mode is defined as the open system call.


#include <sys/types.h>
#include <unistd.h>
int chown (const char *path, uid_t owner, gid_t Group);
chmod system call is used to change the owner of a file. The call uses a numeric value for the user ID and group ID (obtained via GETUID and Getgid calls)


#include <unistd.h>
int unlink (const char *path);
int link (const char *path1, const char *path2);
int symlink (const char *path1, const char *path2);
Unlink system calls delete a directory entry for a file and reduce its number of links. Returns 0 on success, 1 on failure. To use this system transfer to successfully delete a file, you must have write and execute permissions to the directory where the file belongs. If the number of links to a file is reduced to zero, and no process opens it, the file is deleted.
Use the link system to invoke a new link to create a file in the program. It will create a new link to the existing file path1, and the new directory entry is given by path2.
Use the Symlink system call to create symbolic links in a similar way, noting that a file's compliance link does not increase the number of links to that file.


#include <sys/types.h>
#include <sys/stat.h>
int mkdir (const char *path, mode_t mode);
The mkdir system call is used to create the directory, which takes the parameter path as the name of the new directory. The permissions of the directory are set by the parameter mode, and the meaning will be set in the relevant definition of the O_CREAT option called by the Open system. Of course, you also need to obey the umask setting situation.


#include <unistd.h>
int rmdir (const char *path);
RmDir system calls are used to delete a directory, but only if the directory is empty.


#include <unistd.h>
int chdir (const char *path);
The CHDIR system call is used to change the working directory.


#include <unistd.h>
Char *getcwd (char *buf, size_t size);
The GETCWD system call is used to write the name of the current directory into the given buffer buf. If the length of the directory name exceeds the buffer length given by the parameter size (a erange error), it returns NULL, and if successful, it returns the pointer buf. It is important to note that GETCWD may also return NULL if the directory is deleted (einval error) or if the permission has changed (eaccess error) during the program's run.


#include <sys/types.h>
#Include <dirent.h>
DIR *opendir (const char *name);
The purpose of the Opendir function is to open a directory and create a directory stream. If successful, it returns a pointer to the DIR structure that is used to read the catalog data item. Opendir returns a null pointer (NULL) on failure.


#include <sys/types.h>
#Include <dirent.h>
struct Dirent *readdir (DIR *dirp);
The Readdir function returns a pointer to the structure that holds the information about the next directory item in the directory stream dirp. Subsequent Readdir calls will return subsequent catalog entries. If an error occurs or reaches the end of the directory, READDIR returns NULL. When the end of the directory is reached, NULL is returned, but the value of errno is not changed, and errno is set only if an error occurs. It is important to note that if the Readdir function scans the directory while other processes are creating or deleting files in that directory, Readdir will not be guaranteed to list all the files (and subdirectories) in that directory.


#include <sys/types.h>
#Include <dirent.h>
Long int telldir (DIR *dirp);
The return value of the Telldir function records the current position in a directory stream.


#include <sys/types.h>
#Include <dirent.h>
void Seekdir (DIR *dirp, long int loc);
The role of the Seekdir function is to set directory entry pointers for directory stream dirp. The LOC value is used to set the pointer position, which is obtained through the TELLDIR call.


#include <sys/types.h>
#Include <dirent.h>
int Closedir (DIR *dirp);
The purpose of the Closedir function is to close a directory stream and release the resources associated with it. Execution successfully returns 0, and returns-1 if an error occurs.


Other stream functions:

int Fgetpos (FILE *stream, fpos_t *pos);
Get the current (read and write) location of the file stream

int Fsetpos (FILE *fp, const fpos_t *pos);
Sets the current (read and write) location of the file stream

Long Ftell (FILE *fp);
Returns the offset value of the current (read-write) location of the file stream

void Rewind (FILE *stream);
Rewind: Resetting the read-write location in the file stream

file *freopen (const char *path, const char *mode, file *stream);
Implements redirection to direct a predefined standard stream file to a file specified by path.

void Setbuf (FILE *stream,char *buf);
void Setvbuf (FILE *stream,char *buf,int type,unsigned size);
Sets the buffering mechanism for the file stream, which allows the user to create their own file buffers without using the fopen () function to open the default buffers set by the file. For the Setbuf () function, buf indicates that the buffer length is determined by the value of the macro bufsize defined in the header file Stdio.h, with a default value of 512 bytes. When the selected buf is empty, the SETBUF function causes the file I/O to be buffered. For the SETVBUF function, a buffer is allocated by the malloc function. The parameter size indicates the length of the buffer (must be greater than 0), and the parameter type represents the type of buffering, and its value can take the following values:
Type value meaning
_IOFBF files are fully buffered, that is, when the buffer is full, the file can be read and written
_IOLBF file line buffer, that is, when a buffer receives a newline character, it can read and write to the file.
_IONBF file does not buffer, ignoring the value of buf,size, directly read and write files, no longer through the file buffer buffer

int Remove (char *path);
Equivalent to the Unlink function, but if its path parameter is a directory, its role is equivalent to the RMDIR function


Linux/unix File manipulation functions

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.