文章目录
一、标准流
c语言程序运行起来就
默认打开
3个流标准输出流 - stdout(屏幕打印,这里屏幕就是编译器调试控制台)
标准输入流 - stdin(键盘)
标准错误流 - stderr
这几个流类型都是FILE*
fputs向一般文件(磁盘)或者硬件进行写入
#include <stdio.h>
int main()
{
const char* s = "hello world\n";
fputs(s, stdout);
return 0;
}
stderr也是可以输出到屏幕上的
但重定向输出到txt文件中就什么都没有,因为> 是输出重定向的符号
./test > test.txt 2>&1
//原本1指向显示器,重定向指向文件,再将2拷贝到1里面,2也指向文件了
二、linux系统提供操作系统调用接口
打开
关闭
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
int open(const char *pathname, int flags);
int open(const char *pathname, int flags, mode_t mode);
//打开文件的文件名 以读、写等方式打开 以什么模式(权限) //返回文件描述符, 错误返回-1
int creat(const char *pathname, mode_t mode);
#include <unistd.h>
int close(int fd);
1. open创建文件
open的返回值是操作系统给予的,实际上是操作系统内部一个数组的下标
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
int main()
{
int fd = open("./test.txt", O_WRONLY | O_CREAT, 0644);
close(fd);
return 0;