当前位置: 首页> 汽车> 行情 > 微信开放平台在哪里进入_it运维服务外包_阿里云建站_如何自己编写网站

微信开放平台在哪里进入_it运维服务外包_阿里云建站_如何自己编写网站

时间:2025/7/9 3:50:29来源:https://blog.csdn.net/weixin_45925859/article/details/146406767 浏览次数: 0次
微信开放平台在哪里进入_it运维服务外包_阿里云建站_如何自己编写网站

1.标准IO 和 文件IO 的区别

    1.1标准IO(fopen、fclose等)

  •  是库函数
  •  是对系统调用的封装
  • 标准IO有缓存

   1.2文件IO(open、close、write、read)  

  •  是系统调用
  •  是Linux内核函数接口
  •  文件IO没有缓存

标准IO对文件IO进行了封装,标准IO库函数是构建于文件IO这些系统调用之上的。例如:标准IO库函数fopen,就利用系统调用文件IO的open函数来执行打开文件的操作。

可移植性:标准IO有更好的可移植性。

性能、效率:标准IO库在用户空间维护了自己的stdio缓冲区,所以标准IO带有缓冲区,文件IO在用户空间是不带缓存的。标准IO在性能效率上要优于文件IO。

注:Linux系统下的man手册使用

man 1 ls //数字1表示查看Linux命令

man 2 open //数字2表示查看系统调用函数

man 3 fopen //数字3表示查看标准C库函数

2.open打开文件

功能:

打开文件,并获得文件描述符

函数原型:

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>/*  参数:pathname‌: 文件路径。
‌    flags‌: 标志位,控制打开方式。O_RDONLY	只读O_WRONLY	只写O_RDWR		读写O_APPEND	追加O_CREAT		创建O_TRUNC		清0O_ASYNC		异步IOO_NONBLOCK	非阻塞IO‌    mode‌: 创建文件时的权限(需与O_CREAT一起使用)4:读权限(r),2:写权限(w),1:执行权限(x)例如:664:表示本人权限读写,同组人权限读写,其他人仅读权限返回值:成功返回新文件描述符失败返回-1 文件描述符:很小的非负整数 0 1 2 3 ..   0-1023新生成的文件描述符永远为最小的非负整数*/int open(const char *pathname, int flags);
int open(const char *pathname, int flags, mode_t mode);

fopen 与 open 函数读写、执行对应关系

fopen调用:                open调用:

            "r"       -》      O_RDONLY 
            "r+"     -》      O_RDWR 
            "w"      -》      O_WRONLY | O_CREAT | O_TRUNC, 0664
            "w+"    -》     O_RDWR | O_CREAT | O_TRUNC, 0664
            "a"       -》     O_WRONLY | O_CREAT | O_APPEND, 0664
            "a+"     -》    O_RDWR | O_CREAT | O_APPEND, 0664 

2.1 open打开文件及返回值结果验证

程序:

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>int main(int argc, const char *argv[])
{int fd = 0;close(0);while (1){//只写打开文件,不存在创建,存在清0fd = open("./file.txt", O_WRONLY | O_CREAT | O_TRUNC, 0664);if (-1 == fd){perror("fail to open");return -1;}printf("fd = %d\n", fd);}return 0;
}

运行结果:(1)文件清0验证

(2)返回值验证

3.close关闭文件

功能:

用于关闭已打开的文件描述符,释放系统资源并确保数据同步

函数原型:

#include <unistd.h>/*
参数:fd:要关闭的文件描述符(由open()、pipe()、socket()等函数返回。返回值
‌成功‌:返回0。
‌失败‌:返回-1,并设置errno(如EBADF、EINTR等)
*/int close(int fd);

3.1Linux标准输入、输出、错误

在Linux系统中,输入输出(I/O)是程序与外部设备(如键盘、显示器、文件等)交互的基础。Linux提供了几种标准方法来处理输入输出,主要包括以下几种:

  • 标准输入(Standard Input, 简称stdin):通常指的是文件描述符0,用于程序接收外部输入。例如,键盘输入。(0 :标准输入)
  • 标准输出(Standard Output, 简称stdout):通常指的是文件描述符1,用于程序向外部输出信息。例如,控制台输出。(1 :标准输出)
  • 标准错误(Standard Error, 简称stderr):通常指的是文件描述符2,用于程序向外部输出错误信息。(2 :标准错误)

3.2 close函数验证

3.2.1close函数关闭标准输入(0)

程序:

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>int test1()
{printf("test1 \n");int fd = 0;close(0); //关闭标准输入,键盘不能输入printf("请输入循环次数 \n");scanf("%d", &fd);printf("输入循环次数:%d \n",fd);return 0;
}int main(int argc, const char *argv[])
{int num = test1();printf("test1 num:%d \n",num);return 0;
}

运行结果:(1)程序:不屏蔽,执行 close(0);

(2)程序:屏蔽,不执行 close(0);

3.2.2 close函数关闭标准输出(1)

程序:

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>int test2()
{printf("test1 \n");int fd = 0;//close(1); //关闭标准输出,终端不能输出打印数据printf("请输入循环次数 \n");scanf("%d", &fd);printf("输入循环次数:%d \n",fd);return 0;
}int main(int argc, const char *argv[])
{int num = test2();printf("test2 num:%d \n",num);return 0;
}

运行结果:(1)程序:不屏蔽,执行 close(1);

(2)程序:屏蔽,不执行 close(1);

3.2.3 close函数关闭标准出错(2)

程序:

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>int test3()
{printf("test3 \n");int fd = 0;close(2); //关闭标准出错,终端不能输出错误信息//只写打开文件,不存在创建,存在清0//fd = open("./file2.txt", O_WRONLY | O_CREAT | O_TRUNC, 0664);//只读打开file2.txt ,目录下并没有该文件fd = open("./file2.txt", O_RDWR);if (-1 == fd){perror("fail to open file ");return -1;}return 0;
}int main(int argc, const char *argv[])
{int num = test3();printf("test3 num:%d \n",num);return 0;
}

运行结果:(1)程序:屏蔽,不执行 close(2);

出错信息正常打印了:fail to open file : No such file or directory

(2)程序:不屏蔽,执行 close(2);

test3(); 执行后,返回值为 -1 ,已出错。未打印出错信息。

关键字:微信开放平台在哪里进入_it运维服务外包_阿里云建站_如何自己编写网站

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

责任编辑: