SD写函数写不进去,求助高手~~~~

2019-03-24 08:20发布

这里用的f_write是例程中自带的,应该没有错吧。可是不知道为什么就是写不进去。
例如试验如下:
A.txt原有内容是zyz
在超级终端输入命令:
write a.txt china
显示是写成功的,可是打开a.txt。里面的内容却不是china,而是原来的数据。哪位前辈帮帮忙忙看看?

int
Cmd_write(int argc, char *argv[])
{

    FRESULT fresult;
    unsigned short usBytesWrite=0;

         if(strlen(g_cCwdBuf) + strlen(argv[1]) + 1 + 1 > sizeof(g_cTmpBuf))
    {
        UARTprintf("Resulting path name is too long ");
        return(0);
    }
//这一句的作用是判定将要写的文件添加到当前工作路径后,路径长度会不会超过缓冲区大小。

        strcpy(g_cTmpBuf, g_cCwdBuf);
       //将当前路径拷贝到缓冲区。


    if(strcmp("/", g_cCwdBuf))
    {
        strcat(g_cTmpBuf, "/");
    }
    //在路径后面加上一个   /   分隔符
  

    strcat(g_cTmpBuf, argv[1]);
  //将要写的文件名添加到当前工作路径后面

    fresult = f_open(&g_sFileObject, g_cTmpBuf, FA_WRITE);
        //打开文件,用来写。
       
    if(fresult != FR_OK)
    {        UARTprintf("open error ");
        return(fresult);
        }
        //如果打开过程中有错,就返回一个错误。


         strcpy(g_cTmpBuf, argv[2]);       
        //将要写的内容拷贝到缓冲区   
   

     do
         {
     fresult = f_write(&g_sFileObject,g_cTmpBuf,sizeof(g_cTmpBuf) - 1,
                         &usBytesWrite);

        //写文件

        if(fresult != FR_OK)
        {
            UARTprintf("write error ");
            return(fresult);
        }

        //
        // Null terminate the last block that was read to make it a
        // null terminated string that can be used with printf.
        //
   
                }while(usBytesWrite!=sizeof(g_cTmpBuf) - 1);
     //循环写入,直到写完。
        //
        // Print the last chunk of the file that was received.
        //
        UARTprintf("%s", g_cTmpBuf);


    //
    // Continue reading until less than the full number of bytes are
    // read.  That means the end of the buffer was reached/

    //
    // Return success.
    //
    return(0);

} 此帖出自小平头技术问答
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
10条回答
youki12345
2019-03-25 00:23
 精彩回答 2  元偷偷看……0人看过

一周热门 更多>

相关问题

    相关文章