嵌入式技术论坛
直播中

青sky

8年用户 1181经验值
擅长:模拟技术
私信 关注
[问答]

DFS创建文件失败是为什么


显示已经关闭成功了,为什么还是没有申请到新的fd.

代码:
int Write2SD(char *C_data)
{
    static int can_filecount = 00000001;//定义一个静态变量用来新建存储can数据的文件。
    static int can_datacount = 0;//用来记录一个文件里面的帧数
    static uint8_t Creatfile= 1;//用来表示是否需要创建文件
    static uint8_t Creatdir = 1;//用来表示是否需要创建文件夹
    static int fd = 0;
    int res_sync;
    int res_write;
    int res_close;
    //这个函数只负责新建文件夹和新建文件存储数据。
    //创建文件夹
    if(Creatdir == 1)
    {
        int res_mk;
        res_mk = mkdir("/can_data01",0x777);//创建了can_data01文件夹
        if (res_mk < 0)
        {
           /* 创建目录失败 */
           rt_kprintf("dir error!n");
        }
        else {
            //创建成功,下次进来之后就不再创建;
            Creatdir = 0;
        }
    }
    if(Creatfile == 1)
    {
        char name[50] = "/can_data01/can";
        char file_type[] = ".csv";
        char count2char[10];
        count2char[0] = '';
        sprintf(count2char,"%d",can_filecount);
        strcat(name,count2char);
        strcat(name,file_type);
        opendir("/can_data01");
        fd = open(name, O_CREAT|O_WRONLY);//如果文件不存在则创建一个文件
        if(fd>0)
        {
            Creatfile = 0;//创建成功以后,再次进入函数不再创建文件。
        }
        else {
            rt_kprintf("create file failedrn");
        }
    }
    if(fd>0)
    {
       // rt_kprintf("C_data:%srn",C_data);
        res_write = write(fd, C_data, strlen(C_data));
        if(res_write >0)
        {
            ;
           // rt_kprintf("res_write = %drn",res_write);
        }
        else
        {
            rt_kprintf("write error%d rn",strlen(C_data));
            rt_kprintf("write error rn");
        }
        lseek(fd,0, SEEK_CUR);
        res_sync = fsync(fd);//同步,将缓存写到SD卡里面。
        can_datacount++;
        if(can_datacount>100)
        {
            can_datacount = 0;//清零计数器
            Creatfile = 1;//下次进入函数要创建新文件
            can_filecount++;//新建文件的编号
            res_close = close(fd);//写完一个文件一定要关闭;
            if(res_close == 0)
            {
                rt_kprintf("close successed:%drn",res_close);
            }
            else {
                rt_kprintf("close failed:%drn",res_close);
            }
            rt_kprintf("file count:%d,fd = %drn",can_filecount-1,fd);
        }
        if(res_sync == 0)
        {
            ;
        }
        else {
            rt_kprintf("res_sync failed:%d",res_sync);
        }
    }
}

回帖(2)

李敏

2022-4-12 10:06:54
[img]https://oss-club.rt-thread.org/uploads/20220411/66d5fa5be3e564064df9f6d236f75983.png[/img]
显示已经关闭成功了,为什么还是没有申请到新的fd.
[img]https://oss-club.rt-thread.org/uploads/20220411/fb663ee05289736af0f222b873291e66.png[/img]
代码:
int Write2SD(char *C_data)
{
    static int can_filecount = 00000001;//定义一个静态变量用来新建存储can数据的文件。
    static int can_datacount = 0;//用来记录一个文件里面的帧数
    static uint8_t Creatfile= 1;//用来表示是否需要创建文件
    static uint8_t Creatdir = 1;//用来表示是否需要创建文件夹
    static int fd = 0;
    int res_sync;
    int res_write;
    int res_close;
    //这个函数只负责新建文件夹和新建文件存储数据。
    //创建文件夹
    if(Creatdir == 1)
    {
        int res_mk;
        res_mk = mkdir("/can_data01",0x777);//创建了can_data01文件夹
        if (res_mk < 0)
        {
           /* 创建目录失败 */
           rt_kprintf("dir error!
");
        }
        else {
            //创建成功,下次进来之后就不再创建;
            Creatdir = 0;
        }
    }
    if(Creatfile == 1)
    {
        char name[50] = "/can_data01/can";
        char file_type[] = ".csv";
        char count2char[10];
        count2char[0] = '';
        sprintf(count2char,"%d",can_filecount);
        strcat(name,count2char);
        strcat(name,file_type);
        opendir("/can_data01");
        fd = open(name, O_CREAT|O_WRONLY);//如果文件不存在则创建一个文件
        if(fd>0)
        {
            Creatfile = 0;//创建成功以后,再次进入函数不再创建文件。
        }
        else {
            rt_kprintf("create file failed
");
        }
    }
    if(fd>0)
    {
       // rt_kprintf("C_data:%s
",C_data);
        res_write = write(fd, C_data, strlen(C_data));
        if(res_write >0)
        {
            ;
           // rt_kprintf("res_write = %d
",res_write);
        }
        else
        {
            rt_kprintf("write error%d
",strlen(C_data));
            rt_kprintf("write error
");
        }
        lseek(fd,0, SEEK_CUR);
        res_sync = fsync(fd);//同步,将缓存写到SD卡里面。
        can_datacount++;
        if(can_datacount>100)
        {
            can_datacount = 0;//清零计数器
            Creatfile = 1;//下次进入函数要创建新文件
            can_filecount++;//新建文件的编号
            res_close = close(fd);//写完一个文件一定要关闭;
            if(res_close == 0)
            {
                rt_kprintf("close successed:%d
",res_close);
            }
            else {
                rt_kprintf("close failed:%d
",res_close);
            }
            rt_kprintf("file count:%d,fd = %d
",can_filecount-1,fd);
        }
        if(res_sync == 0)
        {
            ;
        }
        else {
            rt_kprintf("res_sync failed:%d",res_sync);
        }
    }
}

举报

刘娟

2022-4-12 10:07:12
opendir中会调用fd_new(), 分配一个fd,但是没有closedir配合,内存泄漏了。
举报

更多回帖

发帖
×
20
完善资料,
赚取积分