Day5.目录IO的基本函数和自定义头文件

自定义头文件示例:

定义好了之后调用规范:
#include "head.h"
注意:文件名要用双引号 不能用尖括号

注意:" "和< >的区别

引用自定义头文件的格式是#include "自定义头文件名.h"
"":从用户的工作路径下搜索头文件
<>:从标准库路径下搜索头文件

主函数传参:

int main(int argc,const char *argv[])
int main(int argc,const char **argv)

参数:

1.argc是int类型,表示运行程序的时候给main函数传递了几个参数

2.argv是一个字符串数组,这个数组用来存储argc个字符串,
  每个字符串就是我们给main函数传的一个参数

本质:

1.main函数传参都是通过字符串传进去的
2.程序被调用时传参,各个参数之间是通过空格来间隔的
3.在程序内部如果要使用argv,那么一定要先检验argc

示例 :

利用read和write实现文件内容的拷贝(将src.bmp中的内容拷贝到dst.bmp文件中)

#include "head.h"

int main(int argc,const char *argv[])
{
	int fscr = 0;
	int fdst = 0;
	char tmpbuff[4069] = {0};
    ssize_t nret = 0;

	if (argc != 3)
	{
		fprintf(stderr,"Usage:./a.out srcfilename dstfilename\n");
		return -1;
	}

	fscr = open(argv[1],O_RDONLY);
	if (fscr == -1)
	{
		perror("fail to open!");
		return -1;
	}

	fdst = open(argv[2],O_RDWR | O_CREAT | O_TRUNC, 0664);
	if (fdst == -1)
	{
		perror("fail to open!");
		return -1;
	}

	while(1)
	{
		nret = read(fscr,tmpbuff,sizeof(tmpbuff));
		if (nret <= 0)
		{
			break;
		}
		write(fdst,tmpbuff,nret);
	}

	close(fdst);
	close(fscr);

	return 0;
}
(将src.bmp拷贝到dst.bmp中)
编译结束后,运行代码参数:
./a.out src.bmp dst.bmp

#include "haed.h"
#include <string.h>

typedef struct persun
{
	char name[8];
	char birthday[15];
}Persun_t;

int main(void)
{
	 Persun_t a[100000];
	 int n = 0;
	 int i = 0;
	 int cot = 0;
	 char maxvalue[15] = {"2014/09/06"};
	 char minvalue[15] = {"1814/09/06"};
	 int maxnew = 0;
	 int minnew = 0;

	 scanf("%d",&n);

	 for (i = 0; i < n; i++)
	 {
		 scanf("%s%s",a[i].name,a[i].birthday);
	 }

	 for (i = 0; i < n; i++)
	 {
		 if (strcmp(a[i].birthday,maxvalue) <= 0 && strcmp(a[i].birthday,minvalue)>=0)
		 {
			 cot++;

			 if (cot == 1)
			 {
				 maxnew = minnew = i;
			 }
			 if (strcmp(a[i].birthday,a[maxnew].birthday)>0)
			 {
				 maxnew = i;
			 }
			 if (strcmp(a[i].birthday,a[minnew].birthday)<0)
			 {
				 minnew = i;
			 }
		 }
	 }
	 printf("%d %s %s\n",cot,a[minnew].name,a[maxnew].name);

	return 0;
}

文件IO

1.lseek

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

功能:

  重新设定文件描述符的偏移量

参数:
  fd:文件描述符

  offset:偏移量

  whence:SEEK_SET  文件开头

                  SEEK_CUR 文件当前位置

                  SEEK_END 文件末尾

返回值:

  成功返回偏移量

  失败返回-1

示例:

#include "head.h"

int main(void)
{
	int  fd = 0;
	char ch = 0;
	off_t len = 0;

	fd = open("b.txt",O_WRONLY | O_CREAT | O_TRUNC, 0664);
	if (fd == -1)
	{
		perror("fail to open!");
		return -1;
	}

	len = lseek(fd,15,SEEK_SET);
	printf("len=%ld\n",len);
	ch = 'a';
	write(fd,&ch,1);

	len = lseek(fd,-3,SEEK_CUR);
	printf("len=%ld\n",len);
	ch = 'b';
	write(fd,&ch,1);

	len = lseek(fd,0,SEEK_SET);
	printf("len=%ld\n",len);
	ch = 'h';
	write(fd,&ch,1);

	close(fd);

	return 0;
}

目录IO

1.mkdir

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

功能:

创建目录文件

参数:
pathname:文件路径

mode:文件的权限

返回值:

成功返回0;

失败返回-1;

rwx rwx rwx
111 111 111
0777
r:目录中是否能够查看文件
w:目录中是否能够新创建文件
x:目录中是否能够进入

2.rmdir

int rmdir(const char *pathname);

功能:

删除空文件目录

返回值:

成功返回0;

失败返回-1;

示例:

#include "head.h"

int main(void)
{
	mkdir("dir", 0777);
	
	rmdir("dir");

	return 0;
}

3.opendir

DIR *opendir(const char *name);

功能:

打开目录获得目录流指针

参数:

name:目录文件路径

返回值:

成功返回目录流指针

失败返回NULL

4.closedir

int closedir(DIR *dirp);

功能:

关闭目录流指针

5.readdir

struct dirent *readdir(DIR *dirp);

功能:

从目录流中读取下一个目录项的结构体信息

参数:

dirp:目录流指针

返回值:

成功返回包含目录项的信息的空间首地址

失败返回NULL

读到文件末尾返回NILL

 struct dirent {
               ino_t          d_ino;       /* Inode number */
               off_t          d_off;       /* Not an offset; see below */
               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]; /* Null-terminated filename */
           };

示例:

打开”dir“目录 读取里面的的信息

#include "head.h"

int main(void)
{
	DIR *dp = NULL;
	struct dirent *pp = NULL;

	dp = opendir("dir");
	if (NULL == dp)
	{
		perror("fail to opendir");
		return -1;
	}

	while (1)
	{
		pp = readdir(dp);
		if (NULL == pp)
		{
			break;
		}

		if ('.' == *pp->d_name)
		{
			continue;
		}

		printf("%s/%s\n", "dir", pp->d_name);
	}

	closedir(dp);

	return 0;
}

6.chdir

int chdir(const char *path);

功能:

切换当前代码的工作路径

示例:

#include "head.h"

int main(void)
{
	chdir("..");
	mkdir("dirname", 0777);

	return 0;
}
上面的程序运行的结果是将"dirname"目录创建的到当前目录的上一级目录中

7.getcwd

char *getcwd(char *buf, size_t size);

功能:

获得当前代码的绝对路径

参数说明:

getcwd()会将当前工作目录的绝对路径复制到参数buffer所指的内存空间中,参数size为buf的空间大小。

示例:

获得当前目录的路径和上一级目录的路径并打印出来 

#include "head.h"

int main(void)
{
	char tmpbuff[4096] = {0};

	getcwd(tmpbuff, sizeof(tmpbuff));
	printf("tmpbuff = %s\n", tmpbuff);

	chdir("..");
	getcwd(tmpbuff, sizeof(tmpbuff));
	printf("tmpbuff = %s\n", tmpbuff);

	return 0;
}

练习:

递归遍历打印任意目录下的所有文件路径

#include "head.h"

int ListDir(const char *pdirname)
{
	DIR *dp = NULL;
	struct dirent *pp = NULL;
	char tmpbuff[4096] = {0};

	dp = opendir(pdirname);         //打开我们需要遍历的目录 并用目录流指针接收
	if (dp == NULL)                       //查看这个目录是否为空 
	{
		perror("fail to opendir!");
		return -1;
	}

	while(1)                  
	{ 
		pp = readdir(dp);              //读出这个目录信息 并用结构体接收
		if (pp == NULL)             //如果为空 则跳出循环
		{
			break;
		}

		if ('.' == *pp->d_name)       //如果度到的这的目录是以'.'开头 则跳过 进入下一次循环
		{
			continue;
		}

		sprintf(tmpbuff,"%s/%s",pdirname,pp->d_name);       //将读到的目录的名字和 我们要查看的目录拼接起来形成一个路径放到tmpbuff这个数组中
		printf("%s\n",tmpbuff);                                        //打印这个数组

		if (pp->d_type == DT_DIR)   //如果这个文件的类型是目录类型
		{
			ListDir(tmpbuff);   //继续递归
		}
	}
	closedir(dp);

	return 0;
}

int main(int argc,const char *argv[])
{
	if (argc != 2)
	{
		fprintf(stderr,"Usage:./a.out dirname\n");
		return -1;
	}

	ListDir(argv[1]);

	return 0;
}

8.access

int access(const char *pathname, int mode);

功能:

检测调用函数的程序对文件是否拥有指定权限

参数;

pathname:文件路径

mode:R_OK  检测是否拥有读的权限

            W_OK  检测是否拥有写权限

             X_OK  检测是否拥有执行权限

             F_OK  检测文件是否存在

返回值:

有该权限返回0

出错返回-1

示例:

#include "head.h"

int main(int argc, const char *argv[])
{
	int ret = 0;

	if (argc != 2)
	{
		fprintf(stderr, "Usage:./a.out filename\n");
		return -1;
	}

	ret = access(argv[1], F_OK);
	if (0 == ret)
	{
		printf("该文件存在!\n");
	}
	else 
	{
		printf("该文件不存在!\n");
	}

	return 0;
}
  • 30
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

我爱敲代码yx

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值