C语言学习笔记:文件IO

一、行缓冲区 - \n

1. 行缓冲 -> 当程序死循环时"Case 1"可显示在终端;"Case 2"打印不出来

#include <stdio.h>

int main(void){
   
	// Case 1 -> 行缓冲区
	printf("Hello World\n");
	// Case 2 -> 没有换行符
	printf("Hello World");
	while (1){
   
	}
	return 0;
}

运行结果:

$ ./a
Hello World

2. 行缓冲 -> 同理"Case 1"直接显示;"Case 2"过三秒跳出循环后才能打印

#include <stdio.h>

int main(void){
   
	// Case 1 -> 行缓冲区
	printf("Hello China\n");
	// Case 2 -> 没有换行符
	printf("Hello World");
	while (1){
   
		sleep(3);
		break;
	}
	return 0;
}

运行结果:

$ ./a
Hello World
Hello World$

二、创建文件 - creat()

/*creat*/
*************************************************************************
	> 头文件	->	#include <sys/types.h>
			->	#include <sys/stat.h>
			->	#include <fcntl.h>
	> int creat(const char * pathname, mode_t mode);
	> int creat(文件路径名,文件访问权限);
	> 参数一	->	pathname	->	创建文件的路径
	> 参数二	->	mode		->	指定创建文件的访问权限	
								->	r:4, w:2, x:1
	> 返回值(成功)	->	返回打开文件的描述符
	> 返回值(失败)	->	出错返回-1并设置errno
	> 作用	->	创建一个文件并以只写的方式打开
			->	如果原来该文件存在,会将这个文件的长度截短为0
*************************************************************************

1. creat -> 新建一个文件 -> 若文件已存在则内部数据清空

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>  // errno
#include <string.h> // strerror

int main(int argc, char *argv[]){
   
	int fd = -1;
	// fd --> 文件描述符
	//	   -> 一个进程打开都会启动3个文件:标准输入(0),标准输出(1),标准出错处理(2)
	//	   -> 因此新建的第一个文件fd为(3)
	fd = creat(argv[1], 0664);
	if (-1 == fd){
   
		// perror中的字符串实参原样打印
		// creat file: Permission denied
		perror("creat file");
		// strerror:将错误编号对应的错误信息返回
		printf("errno = %d, error = %s\n", errno, strerror(errno));
	}else{
   
		printf("open ok --> ret val = %d\n", fd);
	}
	return 0;
}

运行结果:

$ ./a a
creat file: Text file busy
errno = 26, error = Text file busy
$ ./a b
open ok --> ret val = 3
$

2. creat -> 新建多个文件 -> 若文件已存在则内部数据清空

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>  // errno
#include <string.h> // strerror

int main(int argc, char *argv[]){
   
	int fd = -1;
	int i = 0;
	for (i = 1; i < argc; i++){
   
		// 循环体内部为新建一个文件的代码
		fd = creat(argv[i], 0664);
		if (-1 == fd){
   
			printf("errno = %d, error = %s\n", errno, strerror(errno));
		}else{
   
			printf("open %s ok. fd = %d\n", argv[i], fd);
		}	
	}
	return 0;
}

运行结果:

$ ./a a b c d
errno = 26, error = Text file busy
open b ok. fd = 3
open c ok. fd = 4
open d ok. fd = 5
$

3. 文件描述符 -> 内置宏 -> 文件描述符必为正数

文件描述符(fd)
标准输入 0 STDIN_FILENO
标准输出 1 STDOUT_FILENO
标准出错 2 STDERR_FILENO

4. creat -> 内置宏 -> 定义文件的访问权限

r\w\x read write execute
USER S_IRWXU S_IRUSR S_IWUSR S_IXUSR
GROUP S_IRWXG S_IRGRP S_IWGRP S_IXGRP
OTHERS S_IRWXO S_IROTH S_IWOTH S_IXOTH

5. creat -> 内置宏 -> 定义文件的访问权限 -> 说明文件

代码 详细说明
S_IRWXU 00700 user (file owner) has read, write and execute permission
S_IRUSR 00400 user has read permission
S_IWUSR 00200
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值