迅为嵌入式Linux学习笔记2——文件&目录IO

迅为嵌入式Linux学习笔记2——文件&目录IO

open 函数

使用函数需要包含三个头文件

#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);

当 flags 参数中包含 O_CREAT 时,需要使用三个参数,否则使用两个参数。

参数说明:

  • pathname 文件路径名称
  • flags 文件打开方式(常用:O_CREAT、O_RDWR等,可用"|"(或符号连接多个flag))
  • mode 使用O_CREAT时,创建新文件的权限(可读4、可写2、可执行1)

返回值 int : 文件描述符

文件描述符

对于文件IO来说,一切都是围绕文件描述符来进行的。在Linux系统中,所有打开的文件都有一个对应的文件描述符。

文件描述符的本质是一个非负整数,当我们打开一个文件时,系统会给我们分配一个文件描述符。

当我们对一个文件做读写操作的时候,我们使用open函数返回这个文件描述符,标记该文件,并将其作为参数传递给read或者write函数。

在posix.1应用程序里面,文件描述符0,1,2分别对应着标准输入,标准输出,标准出错。

例程1

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

int main(int argc, char *argv[])
{
	int fd;
	fd = open("a.c", O_CREAT | O_RDWR, 0666);//可读可写
	if (fd < 0)
	{
		printf("open is error\n");
	}
	printf("fd is %d\n", fd);
	
	return 0;
}

close函数

使用该函数需要包含头文件

#include <unistd.h>

函数原型:

int close(int fd);

参数:

  • fd 文件描述符
    返回值:
  • 0 关闭成功
  • -1 关闭失败

例程2

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>

int main(int argc, char *argv[])
{
	int fd;
	fd = open("a.c", O_CREAT | O_RDWR, 0666);//可读可写
	if (fd < 0)
	{
		printf("open is error\n");
	}
	printf("fd is %d\n", fd);
	if (fd >= 0)
	{
		close(fd);
	}
	
	return 0;
}

文件描述符的数量有限制,一般是1024个,当打开的文件超过文件描述符数量限制时,新创建的文件描述符将替换原有的文件描述符,原有的文件描述符将失效。

read函数

头文件

#include <unistd.h>

函数原型

ssize_t read(int fd, void *buf, size_t count);

参数:

  • fd 文件描述符
  • buf 缓冲区首地址
  • count 需要读取的字节数

返回值:
读取成功返回实际读取的字节数,如果文件已到末尾,返回0,如果读取失败返回-1

例程3

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>

int main(int argc, char *argv[])
{
	int fd;
	char buf[256] = {0};
	ssize_t ret;
	fd = open("a.c", O_CREAT | O_RDWR, 0666);//可读可写
	if (fd < 0)
	{
		printf("open is error\n");
	}
	printf("fd is %d\n", fd);
	if (fd >= 0)
	{
		ret = read(fd, buf, 256);
		if (ret > 0)
		{
			printf("read succeed\n");
			for (ssize_t i = 0; i < ret; i++)
			{
				printf("%c", buf[i]);
			}
		}
		else
		{
			printf("read error\n");
		}
		close(fd);
	}
	
	return 0;
}

write函数

头文件

#include <unistd.h>

函数原型

ssize_t write(int fd, const void *buf, size_t count);

例程4

#include <unistd.h>

int main(int argc, char *argv[])
{
	write(1, "hello\n", 6);//1 == 标准输出
	return 0;
}

例程5

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>

int main(int argc, char *argv[])
{
	int fd;
	ssize_t ret;
	
	fd = open("a.c", O_RDWR);//注意打开权限,一定要可写,否则 写入将失败
	if (fd < 0)
	{
		printf("open error\n");
		return -1;
	}
	write(fd, "hello\n",6);
	close(fd);
	
	return 0;
}

lseek函数

头文件

#Include <sys/types.h>
#include <unistd.h>

函数原型

off_t lseek(int fd, off_t offset, int whence);

参数:

  • fd 文件描述符
  • offset 偏移量,单位是字节的数量,可以正可以负,如果是负值,表示向前移动,如果是正值,表示向后移动
  • whence 当前位置的基点,可以使用以下三组值

SEEK_SET: 相对于文件开头
SEEK_CUR: 相对于当前的文件读写指针位置
SEEK_END: 相对于文件末尾

返回值:
成功返回当前位移,失败返回-1

举例:

//把文件位置指针设置为100
lseek(fd, 100, SEEK_SET);
//把文件位置指针设置为文件末尾
lseek(fd, 0, SEEK_END);
//获取当前的文件位置
lseek(fd, 0, SEEK_CUR);

mkdir函数

头文件

#include <sys/stat.h>
#include <sys/types.h>

函数原型

int mkdir(const char *pathname, mode_t mode);

参数:

  • pathname 目录名称
  • mode 权限

返回值:
0 创建成功
-1 创建失败

opendir函数

头文件

#include <sys/types.h>
#include <dirent.h>

函数原型

DIR *opendir(const char *name);//DIR *目录流指针

closedir函数

头文件

#include <sys/types.h>
#include <dirent.h>

函数原型

int closedir(DIR *dirp);

readdir函数

头文件

#include <dirent.h>

函数原型

struct dirent *readdir(DIR *dirp);

参数:

  • dirp 目录流指针

返回值:

  • struct dirent * 结构体指针,如果读取失败,返回NULL
struct dirent {
	ino_t	d_ino;	//inode number文件的inode号,每个文件都不同,可以使用 ls -i查看
	off_t d_off;	//not an offset; see Notes
	unsigned short d_reclen;	//length of this record
	unsigned char d_type;	//type of file; not supported by all  filesystem types
	char d_name[256]; //filename 文件名
}

例程6

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

int main(int argc, char *argv[])
{
    int ret;
    DIR *dp;
    struct dirent *dir;

    if (argc != 2)
    {
        printf("Usage %s <name dir> \n", argv[0]);
        return -1;
    }

    dp = opendir(argv[1]);
    if (dp == NULL)
    {
        printf("opendir error\n");
        return -2;
    }
    printf("opendir ok\n");

    while (1)
    {
        dir = readdir(dp);
	    if (dir != NULL)
	    {
	    	printf("file name is %s\n", dir->d_name);
	    } else {
	    	break;
	    }
    }
    
    closedir(dp);
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值