File descriptors and redirections
File reading and writing operations in C language
File writing int fputs(const char *s, FILE *stream); s: the string to be written stream: the corresponding target file to be written
fputs
File output (r can only be run in a file reading environment) char *fgets(char *s, int size, FILE *stream); s: the target location to be saved size: how many bytes to output stream: the output target file
fgets
File writing
**fprintf
int fprintf(FILE *stream, const char *format, ...);
stream: the target file to be written
format: the string to be written
...: represents that multiple groups can be written
snprintf
(Write strings into arrays, not files)**
int snprintf(char *str, size_t size, const char *format, ...);
str: needs to be stored in the target array
size: how large to access
format: corresponding characters string target
Binary file output input
fread
size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream);
fwrite
size_t fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream);
The file writing
fscanf function reads data from the file input stream, stores it in format, and ends when it encounters spaces and newlines.
fscanf
int fscanf(FILE *stream, const char *format, …);
stream: write to the corresponding target file
code
#include <stdio.h>
#include <fcntl.h>
#include <errno.h>
#include <string.h>
#include <sys/stat.h>
#include <unistd.h>
#define LOG "log.text"
int main()
{
//w:默认为写方式打开文件,如果文件不存在,就会创建出来
FILE* fp = fopen(LOG,"w");
if(fp == NULL)
{
perror("fopen");
return 1;
}
//正常进行文件操作
const char* msg = "hello new file";
int cnt = 1;
char buffer[256];
while(cnt)
{
//C语言文件写操作
//snprintf(buffer,sizeof(buffer),"%s:%d:wh\n",msg,cnt);
//printf("%s",buffer);
//fprintf(fp,"%s : %d : wh\n",msg,cnt);
//fprintf(stdout,"%s : %d : wh",msg,cnt);
//fprintf(stdout,msg,NULL);//Linux一切皆文件,stdout也对应一个文件,显示器文件
fputs(msg,fp);
--cnt;
}
char line[128];
//只可以在读的环境下读取 r
if(fgets(line,sizeof(line),fp) == NULL)
{
perror("fgets");
}
else printf("%s",line);
fclose(fp);
return 0;
}
How to open a file in open system
open
#include <fcntl.h>
int open(const char *pathname, int flags);
int open(const char *pathname, int flags, mode_t mode);
pathname: the name of the file to be created
flags: its operation type
- O_RDONLY: open for reading only
- O_WRONLY: Open for writing only
- O_RDWR: open for reading and writing
这三个常量,必须指定一个且只能指定一个
- O_CREAT: If the file does not exist, create it. You need to use the mode option to specify the access permissions of the new file
- O_APPEND: Additional copy
- O_TRUNC: Clear all contents of the file
mode: Set permissions for files
System read and write file operations
File writing
write
#include <unistd.h>
ssize_t write(int fildes, const void *buf, size_t nbyte);
write returns how many bytes are written including \0
文件读出
read
#include <unistd.h>
ssize_t read(int fd, void *buf, size_t count);
close
#include <unistd.h>
int close(int fildes);
file descriptor
But
open
when opening or creating a file, the system will return an integer to us. When -1 is returned, it means that the creation of the file failed. Otherwise, the creation is successful. When the creation is successful, it will also return us an integer value. This value we It's called a file descriptor.
When we modify a file, we use operators.
So what is a file descriptor?
The file descriptor is an array subscript, which represents different meanings.
By default, the Linux process will have three default open file descriptors, namely standard input > 0, standard output 1, and standard error 2. The
physical devices corresponding to 0, 1, and 2 are generally: keyboard, monitor, and monitor.
So when After we create a new file, the descriptor corresponding to each file is 3. This is because the first three are occupied.
File storage rules.
When standard input, output, and error are all occupied, the file will be saved in the order of the array. If the first 0, 1, and 2 appear, the file will be closed. Then when the file is saved, the empty part in front will be given priority.
file redirection
If we don't want to occupy the first three, we can manually change the paths of standard input, output, and error, or we can
dup2
change them through functions.
The common ways of linux redirection are: 1. Standard input and output redirection, using the '>' or '>>' symbol; 2. Standard error redirection, using the '2>' symbol; 3. Input stream redirection, Use the '<' symbol to implement; 4. Pipe redirection, use the '|' symbol to implement.
Manual change
dup2() function modification
int dup2(int oldfd, int newfd);
oldfd overwrites nwefd
How to understand file buffer
The refresh strategy adopted by the monitor is line buffering and
the buffering strategy adopted by the file is full buffering.