目录读写函数

1.fileno函数和fdopen函数:文件类型转换

//fileno
#include <stdio.h>
#include <unistd.h>
int main(int argc, char *argv[])
{
    //FILE*---> int  fd;
    FILE* fp = fopen("1.txt","r");
    if(NULL == fp)
    {
        printf("fopen error\n");
        return 1;
    }

    char buf[256]={0};
    
    int fd = fileno(fp);
    read(fd, buf,sizeof(buf)-1);
    printf("%s\n",buf);
    fclose(fp);
    return 0;
}


//fdopen

#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
int main(int argc, char *argv[])
{
    //FILE*---> int  fd;
    int fd  = open("1.txt",O_RDONLY);
    if(-1 == fd)
    {
        printf("fopen error\n");
        return 1;
    }

    char buf[256]={0};
    FILE* fp = fdopen(fd,"r");
    if(NULL == fp)
    {
        printf("fdopen error\n");
        return 1;
    }
    fgets(buf,sizeof(buf),fp);
    printf("%s\n",buf);
    fclose(fp);
    return 0;
}

2.ls函数:使用opendir,readdir函数获得目录下文件

//基本
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
       #include <dirent.h>

int main(int argc, char *argv[])
{
    DIR *dir = opendir("./");
    if(NULL == dir)
    {
        printf("opendir");
        return 1;
    }

    while(1)
    {
       struct dirent * info =  readdir(dir);
       if(NULL == info)
       {
        break;
       }
       printf("%s\n",info->d_name);
    }
    closedir(dir);
    return 0;
}

//更详细
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
       #include <dirent.h>

int main(int argc, char *argv[])
{
    DIR *dir = opendir("./");
    if(NULL == dir)
    {
        printf("opendir");
        return 1;
    }

    while(1)
    {
       struct dirent * info =  readdir(dir);
       if(NULL == info)
       {
        break;
       }
       switch(info->d_type)
       {

       case DT_DIR:
           printf("目录文件 ");
           break;
       case DT_BLK:
           printf("块设备 ");
           break;
       case DT_CHR:
           printf("字符文件 ");
           break;
       case DT_REG:
           printf("普通文件 ");
           break;
       default:
           printf("其他文件 ");
       }
       printf("%s\n",info->d_name);
    }
    closedir(dir);
    return 0;
}

3.chdir函数:改变目录,创建文件

#include <stdio.h>
#include <unistd.h>
int main(int argc, char *argv[])
{
    int ret = chdir("../");
    if(-1 == ret)
    {
        printf("chdir error\n");
        return 1;
    }

    fopen("aaa","w");
    return 0;
}

4.getcwd函数:获得目录的路径


#include <stdio.h>
#include <unistd.h>
int main(int argc, char *argv[])
{
    char buf[512]={0};
    getcwd(buf,sizeof(buf));
    printf("current path is %s\n",buf);
    int ret = chdir("../");
    if(-1 == ret)
    {
        printf("chdir error\n");
        return 1;
    }
    
    getcwd(buf,sizeof(buf));
    printf("after change path is %s\n",buf);
    return 0;
}

5.mkdir函数:创建文件

#include <stdio.h>
#include <sys/stat.h>
#include <sys/types.h>
int main(int argc, char *argv[])
{
    int ret = mkdir("bbb",0777);  
    if(-1 == ret)
    {
        printf("mkdir error\n");
        return 1;
    }
    return 0;
}

6.rmdir函数:删除文件

#include <stdio.h>
#include <unistd.h>
int main(int argc, char *argv[])
{
    int ret = rmdir("bbb");
    if(-1 == ret)
    {
        printf("rmdir error\n");
        return 1;
    }
    return 0;
}

7.stat函数:获得文件的具体信息

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
int main(int argc, char *argv[])
{
    struct stat st;
    int ret = stat("./01ls.c",&st);
    if(-1 == ret)
    {
        printf("stat error\n");
        return 1;
    }

    printf("ino:%lu mode:%d link:%lu uid:%d gid:%d size:%lu time:%lu\n",st.st_ino, st.st_mode,st.st_nlink 
           ,st.st_uid, st.st_gid,st.st_size,st.st_mtim.tv_sec);

    return 0;
}

8.ll 函数:获得文件权限

#include <stdio.h>
 #include <sys/types.h>
       #include <sys/stat.h>
       #include <unistd.h>
int main(int argc, char *argv[])
{
    struct stat st;
    char filename[]="./01ls.c";
    int ret = stat(filename,&st);
    if(-1 == ret)
    {
        printf("stat error\n");
        return 1;
    }
    /*
    printf("ino:%lu mode:%d link:%lu uid:%d gid:%d size:%lu time:%lu\n",st.st_ino, st.st_mode,st.st_nlink 
           ,st.st_uid, st.st_gid,st.st_size,st.st_mtim.tv_sec);
*/

    // -rw-rw-r-- 1 linux linux  433 Aug  7 11:11 01ls.c 
     // chr c  blk b fifo p sym l sock s 
    // man 7 inode  169line 
    if( S_ISREG(st.st_mode))
    {
        fputc('-',stdout);
    }
    else if(S_ISDIR(st.st_mode)) 
    {
        fputc('-',stdout);
    }
    else if(S_ISCHR(st.st_mode)) 
    {
        fputc('c',stdout);
    }
    else 
    {
        //other
        fputc('o',stdout);
    }
    // -rw-rw-r-- 1 linux linux  433 Aug  7 11:11 01ls.c 
    
    if(st.st_mode & S_IRUSR)
    {
        fputc('r',stdout);
    }
    else 
    {
        fputc('-',stdout);
    }
    if(st.st_mode & S_IWUSR)
    {
        fputc('w',stdout);
    }
    else 
    {
        fputc('-',stdout);
    }
    if(st.st_mode & S_IXUSR)
    {
        fputc('x',stdout);
    }
    else 
    {
        fputc('-',stdout);
    }
    // -rw-rw-r-- 1 linux linux  433 Aug  7 11:11 01ls.c 
    
    printf(" %lu %d %d %lu %lu  %s\n",st.st_nlink,st.st_uid,st.st_gid,st.st_size
           ,st.st_mtim.tv_sec,filename) ;
    return 0;
}

  • 24
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
stm32flash是一个用于在STM32单片机上进行固件编程的开源工具,它提供了一组读写函数,用于操纵STM32内部的闪存存储器。 stm32flash读写函数包括以下几个主要函数: 1. `flash_identify()` 函数用于识别目标单片机的型号和闪存大小,它会发送读标识命令到目标单片机,并解析返回的信息。通过该函数,我们可以获取目标单片机的型号和闪存容量,以便后续的操作。 2. `flash_emit_bootloader()` 函数用于向目标单片机的闪存中编程bootloader程序。该函数会将bootloader的二进制文件读取到内存中,并通过发送编程命令将其写入目标单片机的闪存。 3. `flash_emit_firmware()` 函数用于向目标单片机的闪存中编程应用程序固件。该函数会将应用程序固件的二进制文件读取到内存中,并通过发送编程命令将其写入目标单片机的闪存。 4. `flash_read()` 函数用于从目标单片机的闪存中读取数据。该函数会发送读取命令到目标单片机,并接收返回的数据。我们可以指定要读取的地址和数据长度,以及接收数据的缓冲区。通过该函数,我们可以获取目标单片机闪存中指定地址的数据。 5. `flash_write()` 函数用于向目标单片机的闪存中写入数据。该函数会发送写入命令和要写入的数据到目标单片机。我们可以指定要写入的地址和数据长度,以及要写入的数据。通过该函数,我们可以往目标单片机闪存中指定地址写入数据。 这些函数都是通过与目标单片机进行通信,发送特定的命令来实现相应的功能。通过stm32flash读写函数,我们可以对STM32单片机的闪存进行编程和读取操作,实现固件的更新和数据的读写
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值