IO进程线程 day3 文件IO与目录操作

IO进程线程 day3 文件IO与目录操作_第1张图片

 1.使用标准IO完成两个文件拷贝

#include 
int main(int argc, const char *argv[])
{
	//判断输入是否合法
	if(argc>3)
	{
		printf("输入不合法\n");
		return -1;
	}
	//定义两个文件指针,用于读写
	FILE *fp1=NULL;
	FILE *fp2=NULL;
	if((fp1=fopen(argv[1],"r"))==NULL)
	{
		perror("");
		return -1;
	}
	if((fp2=fopen(argv[2],"w"))==NULL)
	{
		perror("");
		return -1;
	}
	//循环读取搬运
	while(1)
	{
		char buf[20];//定义搬运工
		int res=fread(buf,1,sizeof(buf),fp1);//循环读取字符
		fwrite(buf,1,res,fp2);//将字符拷贝到第二个文件中
		if(feof(fp1))//执行上述操作后,判断光标位置
		{
			break;//如果光标在结尾则退出循环
		}
	}
	//关闭两个文件
	fclose(fp1);
	fclose(fp2);
	return 0;
}
ubuntu@ubuntu:day3$ ls
04write.c  homework1.c  stat.c  test1.c
ubuntu@ubuntu:day3$ gcc homework1.c 
ubuntu@ubuntu:day3$ ./a.out test1.c test2.c
ubuntu@ubuntu:day3$ ls
04write.c  a.out  homework1.c  stat.c  test1.c  test2.c
ubuntu@ubuntu:day3$ 

2.使用文件IO完成两个文件的拷贝

#include 

int main(int argc, const char *argv[])
{
    //判断传入的文件个数
    if(argc != 3)
    {
        printf("input file error\n");
        printf("usage:./a.out srcfile dstfile\n");
        return -1;
    }

    //定义文件描述符变量
    int srcfd, dstfd;

    //以只读的形式打开源文件
    if((srcfd = open(argv[1], O_RDONLY)) ==-1)
    {
        perror("open srcfile error");
        return -1;
    }
    //以只写的形式打开目标文件
    if((dstfd = open(argv[2], O_WRONLY|O_CREAT|O_TRUNC, 0664)) ==-1)
    {
        perror("open dstfile error");
        return -1;
    }
    
    //不断将源文件中的内容读出,写入的目标文件中
    //直到源文件全部读取结束
    char buf[128] = "";
    while(1)
    {

        memset(buf, 0, sizeof(buf));    //将容器清空

        int res = read(srcfd, buf, sizeof(buf));   //从源文件中读取数据

        write(dstfd, buf, res);     //将数据写入目标文件

        //对读取的数据个数进行判断
        if(res == 0)
        {
            break;
        }
    }
    

    //关闭两个文件
    close(srcfd);
    close(dstfd);

    printf("拷贝成功\n");

    return 0;
}
ubuntu@ubuntu:day3$ gcc homework2.c
ubuntu@ubuntu:day3$ ./a.out test2.c test3.c
拷贝成功
ubuntu@ubuntu:day3$ diff test2.c test3.c
ubuntu@ubuntu:day3$ 

3.实现stat函数

#include 

int main(int argc, const char *argv[])
{
    //定义文件属性类型的数据
    struct stat sx;      //用于存储获得的文件属性

    //调用函数的到文件属性
    stat(argv[1], &sx);

    switch(sx.st_mode&S_IFMT)
    {
    case S_IFSOCK:
        {
            printf("套接字文件\t");
        }
        break;
    case S_IFLNK:
        {
            printf("链接文件\t");
        }
        break;
    case S_IFREG:
        {
            printf("普通文件\t");
        }
        break;
    case S_IFBLK:
        {
            printf("块设备文件\t");
        }
        break;
    case S_IFDIR:
        {
            printf("目录文件\t");
        }
        break;
    case S_IFCHR:
        {
            printf("字符设备文件\t");
        }
        break;
    case S_IFIFO:
        {
            printf("管道文件\t");
        }
        break;

    }

    printf("%#o\t%ld\t%ld\n", sx.st_mode&0777, sx.st_size, sx.st_ino);

    return 0;
}
ubuntu@ubuntu:day3$ gcc homework3.c
ubuntu@ubuntu:day3$ ./a.out test2.c
普通文件	0664	6	667814
ubuntu@ubuntu:day3$ 

4.实现目录操作

#include 

int main(int argc, const char *argv[])
{
    //判断外部传参个数
    if(argc != 2)
    {
        printf("input error\n");
        printf("usage:./a.out name\n");
        return -1;
    }
    //定义目录指针
    DIR *dp = NULL;
    //打开目录
    if((dp = opendir(argv[1])) == NULL)
    {
        perror("opendir error");
        return -1;
    }

    //读取目录中的文件或目录信息
    struct dirent *sdp = NULL;
    while((sdp = readdir(dp)) != NULL)
    {
        //输出当前文件或目录的信息
        printf("inode:%10ld, size:%10d, %10s, ",\
                sdp->d_ino, sdp->d_reclen, sdp->d_name);

        //输出类型
        switch(sdp->d_type)
        {
        case DT_BLK:
            {
                printf("b\n");
            }
            break;
    case DT_CHR:
            {
                printf("c\n");
            }
            break;

    case DT_DIR:
            {
                printf("d\n");
            }
            break;

    case DT_FIFO:
            {
                printf("p\n");
            }
            break;
    case DT_LNK:
            {
                printf("l\n");
            }
            break;
    case DT_REG:
            {
                printf("-\n");
            }
            break;
    case DT_SOCK:
            {
                printf("s\n");
            }
            break;
        }
    }
    //关闭目录
    closedir(dp);

    return 0;
}
ubuntu@ubuntu:day3$ gcc homework4.c
ubuntu@ubuntu:day3$ ./a.out .
inode:    655425, size:        24,         .., d
inode:    667813, size:        32, homework4.c, -
inode:    667819, size:        32, homework3.c, -
inode:    667822, size:        32,      a.out, -
inode:    667818, size:        32, homework2.c, -
inode:    667816, size:        32, homework1.c, -
inode:    667815, size:        32,    test1.c, -
inode:    667812, size:        32,     stat.c, -
inode:    667808, size:        24,          ., d
inode:    667814, size:        32,    test2.c, -
inode:    667811, size:        32,  04write.c, -
inode:    667817, size:        32,    test3.c, -
ubuntu@ubuntu:day3$ 

你可能感兴趣的:(嵌入式学习-IO进程线程,c语言)