Vue框架:
Vue驾校-从项目学Vue-1
算法系列博客友链:
神机百炼
函数 | 功能 | 适用流 |
---|---|---|
fgetc | 字符输入函数 | 所有输入流 |
fputc | 字符输出函数 | 所有输出流 |
fgets | 文本行输入函数 | 所有输入流 |
fputs | 文本行输出函数 | 所有输出流 |
scanf | ||
fscanf | 格式化输入函数 | 所有输入流 |
printf | ||
fprintf | 格式化输出函数 | 所有输出流 |
fread | 二进制输入 | 文件 |
fwrite | 二进制输出 | 文件 |
sscanf | 将其他类型数据,转化为格式化数据 | |
sprintf | 将格式化数据,转化为其他类型数据 |
#include
FILE *fopen(const char *path, const char *mode);
/*
r:只读已存在的文件
r+:读写已存在的文件
rb:只读已存在的二进制文件
rb+:读写已存在的二进制文件
w:只写,创建/覆盖文件
w+:可读可写,创建/覆盖文件
wb:只写,创建/覆盖二进制文件
wb+:可读可写,创建/覆盖二进制文件
a:只写,创建/追加文件
a+:可读可写,创建/追加文件
ab:只写,创建/追加二进制文件
ab+:可读可写,创建/追加二进制文件
*/
#include
int main(){
FILE *fp = fopen("文件主干名.文件后缀","打开方式");
if(fp == NULL){
perror("fopen失败");
return -1;
}
fclose(fp);
fp = NULL; //关闭文件后记得将指针指向NULL
return 0;
}
#include
int main(){
FILE* fp = fopen("文件路径",mode);
if(fp == NULL){
perror("fopen失败");
return -1;
}
fclose(fp);
fp = NULL;
return 0;
}
#include
int main(){
FILE* fp = fopen("文件路径",mode);
if(fp == NULL){
perror("fopen失败");
return -1;
}
fputs("hello fputs\n", stdout);
fputs("hello fputs\n", fp);
fclose(fp);
fp = NULL;
return 0;
}
#include
struct test{
char a;
int b;
double c;
};
int main(){
struct test t = {'1', 2, 3};
FILE* fp = fopen("文件路径",mode);
if(!fp){
perror("fopen失败");
return -1;
}
fprintf(fp,"%c %d %lf",t.a, t.b, t.c);
fclose(fp);
fp = NULL;
return 0;
}
#include
#include
int main(){
FILE *fp = fopen("路径","打开方式");
if(!fp){
perror("文件打开失败\n");
return -1;
}
const char *msg = "fwrite()用法示范";
for(int i=0; i<5; i++){
fwrite(msg, 1, strlen(msg), fp);
}
fclose(fp);
fp = NULL;
return 0; //exit()自带流关闭
}
#include
int main(){
FILE* fp = fopen("文件路径",mode);
if(fp == NULL){
perror("fopen失败");
return -1;
}
char arr[20];
fgets(arr, 20, fp); //从文件中读取20个字符
printf("%s\n", arr);
fgets(arr, 20, stdin); //从键盘读取20个字符
printf("%s\n", arr);
fclose(fp);
pf = NULL;
return 0;
}
#include
struct test{
char a;
int b;
double c;
};
int main(){
struct test t;
FILE* fp = fopen("文件路径",mode);
if(fp == NULL){
perror("fopen失败");
return -1;
}
fscanf(fp, "%c %d %lf", &t.a, &t.b, &t.c);
fclose(fp);
fp = NULL;
return 0;
}
#include
#include
int main(){
FILE* fp = fopen("文件路径",mode);
if(!fp){
perror("fopen失败");
return -1;
}
char buffer[1024];
while(1){
ssize_t s = fread(buffer, 1, 24, fp);
if(s > 0){
buffer[s] = 0;
printf("%s\n", buffer);
}
if(feof(fp)) break;
}
fclose(fp);
return 0;
}
== printf()不可以视作是fprintf()固定stdout参数,实时上printf()函数输出对象是fd_array[1] ==
//创建文件时需要赋予权限
int open(const char *pathname, int flags, mode_t mode);
//pathname:路径名
/*flag:打开模式
O_RDONLY: 只读打开
O_WRONLY: 只写打开
O_RDWR : 读,写打开
这三个常量,必须指定一个且只能指定一个
O_CREAT : 若文件不存在,则创建它。需要使用mode选项,来指明新文件的访问权限
O_APPEND: 追加写
这三个变量,可以不指定,但是必须搭配前三个使用
*/
//mode:要赋予该文件的权限,还要在代码中搭配unmask(减去的权限)使用
//打开已有文件时不需要赋予权限
int open(const char *pathname, int flags);
#include
#include
#include
#include
int main(){
umask(0); //不做权限删除
int fd = open("myfile", O_WRONLY|O_CREAT, 0644); //当前文件夹下创建后写入
if(fd < 0){
perror("open失败");
return 1;
}
close(fd);
return 0;
}
#include
#include
#include
#include
int main(){
umask(0); //不做权限删除
int fd = open("myfile", O_WRONLY|O_CREAT, 0644); //当前文件夹下创建后写入
if(fd < 0){
perror("open失败");
return 1;
}
close(fd);
return 0;
}
作用:向fd_array[]中指定fd对应文件中写入内容
参数:fd,内容字节数,内容指针
返回值:本次写入数据的字节数
举例:
#include
#include
#include
#include
#include
#include
int main(){
umask(0);
int fd = open("myfile", O_WRONLY|O_CREAT, 0644);
if(fd < 0){
perror("open");
return 1;
}
const char *msg = "hello world!\n";
for(int i=0; i<5; i++){
write(fd, msg, strlen(msg));
//fd:被读文件描述符。msg:要写入内容首地址。strlen():每次读取的字节数
}
close(fd);
return 0;
}
#include
#include
#include
#include
#include
#include
int main(){
int fd = open("myfile", O_RDONLY);
if(fd < 0){
perror("open");
return 1;
}
const char *msg = "hello world!\n";
char buf[1024];
while(1){
ssize_t s = read(fd, buf, strlen(msg));
if(s > 0){
printf("%s", buf);
}else{
break;
}
}
close(fd);
return 0;
}
1. 背景:每个进程的files_struct中的fd_array[]前三者已经被填充完:标准输入流 / 标准输出流 / 标准错误流
2. 分配规则:进程每通过open()打开一个文件时,选取fd_array[]中**没有对应file且下标最小的值作为访问该文件的下标**
#include
#include
#include
#include
int main(){
int fd = open("文件路径",O_RDONLY);
if(fd<0){
perror("open");
return 1;
}
printf("fd: %d\n", fd);
close(fd);
return 0;
}
#include
#include
#include
#include
int main(){
close(0);
int fd = open("myfile", O_RDONLY);
if(fd < 0){
perror("open");
return 1;
}
printf("fd: %d\n", fd);
close(fd);
return 0;
}
前提:
重定向:
#include
#include
#include
#include
#include
int main(){
close(1); //为fd_array[1]重定向
int fd = open("当前路径下的文件可以直接用文件名", O_WRONLY|O_CREAT, 00644);
if(fd < 0){
perror("open");
return 1;
}
printf("fd: %d\n", fd); //输出为1
fflush(stdout); //刷新缓冲区
close(fd);
exit(0);
}
#include
int dup2(int oldfd, int newfd);
#include
#include
#include
#include
#include
int main(){
int fd = open("log.txt", O_RDWR|O_CREAT, 00644);
if(fd < 0) {
perror("open失败");
return -1;
}
close(1);
dup2(fd,1);
while(1){
char msg[1024];
ssize_t len = read(0, msg, sizeof(msg)-1);
if(len < 0){
perror("read结束");
break;
}
printf("%s", msg);
fflush(stdout);
}
close(fd);
return 0;
}
无缓冲:所有输入直接输出到指定文件
行缓冲:
全缓冲:
#include
#include
#include
#include
#include
#include
int main(){
printf("C语言printf函数\n");
fprintf(stdout, "%s\n", "C语言fprintf函数");
char* msg = "系统调用write接口\n";
write(1, msg, strlen(msg));
fork();
return 0;
}
写时拷贝前:
写时拷贝后:
过程分析: