The previous article is the file operation (chapter III), the code in the article in the file operation (code download).
3.6 formatted output and input 3.6.1 printf, fprintf, and sprintf functions printf functions are capable of formatting and outputting parameters of various types. The representation of each parameter in the output stream is controlled by the format parameter, which is a string containing the normal characters that need to be output and the conversion control code.
#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, and the fprintf function sends its own output to a specified file stream. The sprintf function writes its own output and an end Kong to the string s passed in as a parameter.
printf ("Some numbers:%d,%d and%d\n", 1, 2, 3);
%d,%i Output Decimal integer
%o,%x output octal or hexadecimal integers
%c outputs one character
%s output A string
%d output a floating-point number
%e number of floating-point numbers in the output scientific notation format
%g output (double-precision) floating-point number
The printf function returns an integer that indicates the number of characters it outputs.
The return value of sprintf does not count to the end of the empty character.
The 3.6.2 scanf, fscanf, and SSCANF functions scanf functions to read data from a file stream and
Place the data value in a variable at the address passed in as a pointer parameter .。
#include <stdio.h>
int scanf (const char *format, ...);
int fscanf (file* stream, const char* format,...);
int sscanf (const char* S, Const char* format,..);
In the SCANF series functions, those ordinary characters are used to specify the characters that must appear in the input data. For example:
int num;
scanf ("Hello%d", &num);
This scanf call succeeds only if the next five characters in the standard input match "Hello".
Use the%c control to read a character from the input, and it does not skip the starting white space character.
Using the%s control to scan a string will skip the starting white space character and stop at the first whitespace character appearing in the string, so it is best to use it to read a word instead of a string in the general sense.
Use
%[] control reads a string consisting of a character set and a character in。 The format string%[a-z] reads a string consisting of uppercase letters. If the first character in a character set is ^, it will read a string that does not belong to the character set and the characters in it. Therefore, read a string with a space, and stop when you encounter the first comma, you can use%[^,].
Given the following input line:
Hello, 1234, 5.678, X, string to the end of the line
Char s[256];
int n;
float F;
char c;
scanf ("hello,%d,%g,%c,%[^\n", &n,&f,&c,s);
The return value of the scanf function is the number of data items it successfully reads, and if it fails to read the first data item, its return value will be 0. If the end of the input is reached before the first data item is matched, EOF is returned.
3.6.3 other stream functions The Stdio function library also has some other functions that use flow parameters or standard flows stdin, stdout, and stderr, as follows:
Fgetpos: Get the current (read and write) location of the file stream
Fsetpos: Sets the current (read and write) location of the file stream
Writing COPY_STDIO.C Programs
Run this program and discover that while
less than the underlying database copy version block (COPY_BLOCK.C), but more than that
system calls copy one character at a time with a much faster version (COPY_SYSTEM.C)。 This is
because the Stdio library uses an internal buffer in the file structure, the underlying system calls are made only in the buffer .。
3.6.4 file stream error in order to indicate an error, many Stdio library functions return an out-of-range value, such as a null pointer or EOF constant. At this point, the error is indicated by the external variable errno:
#include <errno.h>
extern int errno;
Because many functions can change the value of errno, its value is only meaningful if the function call fails. It must be checked immediately after the function call has failed.
You can also check the status of the file stream to determine if an error has occurred, or if you have reached the end of the file.
#include <stdio.h>
int ferror (file* stream);
int feof (file* stream);
void Clearerr (file* stream);
3.7 Maintenance standard libraries and system calls for files and directories provide comprehensive support for the creation and maintenance of files and directories.
3.7.1 chmod system calls can be
change the access rights of a file or directory by chmod system calls。 This forms the basis for the shell program chmod.
The function is prototyped as follows:
#include <sys/stat.h>
int chmod (const char* Path, mode_t mode);
The file specified by the path parameter is modified to have access rights given by the mode parameter. The parameter mode is defined in the same way as the open system call, and is also a bitwise OR operation on the required access rights.
3.7.2 chown system Call the Superuser can use the Chown system call to change the owner of a file.
#include <sys/types.h>
#include <unistd.h>
int chown (const char *path, uid_t owner, gid_t Group);
This call uses a numeric value for the user ID and group ID (obtained through GETUID and Getgid calls) and a system value that qualifies who can modify the owner.
3.7.3 unlink, link, and symlink system calls can be used to delete a file using the unlink system call.
Unlink system calls delete a directory entry for a file and reduce its number of links. It returns 0 on success and returns-1 on failure. The function prototypes are as follows:
#include <unistd.h>
int unlink (const char* path);
int link (const char *path1, const char *path2);
int symlink (const char *path1, const char *path2);
if the number of links to a file is reduced to zero, and no process opens it, the file is deleted. In fact, the catalog item is always deleted immediately, but the space occupied by the file waits until the last process (if any) is closed before it is reclaimed by the system. This is the call that the RM program uses.
The link system call will create a new link to the existing file path1. The new catalog item is given by path2.
3.7.4 mkdir and RmDir system calls can be used to create and delete directories using mkdir and RMDIR system call
#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 is equivalent to the MKDIR program, and mkdir calls the parameter as the name of the new directory.
#include <unistd.h>
int rmdir (const char* path);
RmDir system calls are used to delete a directory, but only if the directory is empty.
3.7.5 chdir system calls and GETCWD function programs can browse directories as users do in the file system, just as the shell uses the CD command to switch directories, and the program uses a chdir system call
#include <unistd.h>
int chdir (const char *path);
The program can determine its current working directory by calling the GETCWD function.
#include <unistd.h>
Char *getcwd (char *buf, size_t size);
The GETCWD function writes the name of the current directory to the given buffer buf.
3.8 Scan Directory A common problem with Linux systems is to scan the directory, which is to determine which files are stored in a specific directory. In shell programming, this is easy to do-just let the shell do a wildcard extension of the expression once.
Functions related to directory operations are life in the Dirent.h header file. They use a structure called Dir as the basis for directory operations. Pointers to this structure, called directory streams, are used to perform various directory operations.
The function of the 3.8.1 Opendir function opendir is to open a directory and create a directory stream.
#include <sys/types.h>
#include <dirent.h>
dir* opendir (const char* name);
The 3.8.2 Readdir function Readdir function returns a pointer to a structure that holds information about the next directory item in the directory stream dirp.
#include <sys/types.h>
#include <dirent.h>
struct dirent* readdir (dir* dirp);
The return value of the 3.8.3 Telldir function Telldir function records the current position in a directory stream.
#include <sys/types.h>
#include <dirent.h>
Long int telldir (dir* dirp);
The function of the 3.8.4 Seekdir function Seekdir is to set the directory item pointer dirp the directory stream, and the LOC value is used to set the pointer position, which should be obtained through a telldir call.
#include <sys/types.h>
#include <dirent.h>
void Seekdir (dir* dirp, long int loc);
The 3.8.5 closedir function Closedir function closes a directory stream and frees resources associated with it.
#include <sys/types.h>
#include <dirent.h>
int Closedir (dir* dirp);
Write the PRINTDIR.C program to implement a simple directory listing function.
3.10/proc file System Linux sees everything as a file, and hardware devices have entries in the file system. We use the underlying system to invoke a special way to access the hardware through the files in the/dev directory.
Linux provides a special file system PROCFS, which is usually
the form of the/proc directory. This directory contains a number of special files for higher-level access to driver and kernel information。
For example,/proc/cpuinfo gives detailed information about the CPU.
/proc/meminfo and/proc/version are given the memory usage and kernel version information respectively.
/proc/net/socket file gives the usage statistics of network sockets
A digitally named subdirectory in the/proc directory is used to provide information about a running program.
Each process has a unique identifier: a number in 1-32000. The PS command gives a list of currently running processes.
3.11 Advanced Topics 3.11.1 FCNTL system calls FCNTL system calls provide more ways to manipulate the underlying file descriptors.
#include <fcntl.h>
int fcntl (int fildes, int cmd);
int fcntl (int fildes, int cmd, long arg);
The function of the 3.11.2 mmap function mmap (memory-mapped) functions is to create a memory that can be read and written by two or more programs. The changes that a program makes to it can be seen by other programs.
The MAPP function creates a pointer to a memory area associated with the contents of a file that can be accessed through an open file descriptor.
#include <sys/mman.h>
void *mmap (void *addr, size_t len, int prot, int flags, int fildes, off_t off);
3.12 Summary This chapter focuses on how Linux provides direct access to files and devices, and describes how library functions built on these underlying functions provide a flexible solution for programming problems.
Linux Programming--File Operations (chapter III)