vxworks dosfs文件系统文件读写测试

来源:互联网 发布:java和c语言的区别 编辑:程序博客网 时间:2024/04/27 18:48

/**
  ******************************************************************************
  * @file    dosfsTest.c
  * @author  Daniel_Lee
  * @version V1.0
  * @date    2013.7.15
  * @brief   This is a test of using dosfs.
  ******************************************************************************
**/ 
/* Includes ------------------------------------------------------------------*/

#include "vxWorks.h"

#include "iv.h"

#include "intLib.h"
#include "sysLib.h"
#include "logLib.h"
#include "dosfsLib.h"
#include "ioLib.h"

#include "stdio.h"


STATUS dosfsTest(){
    int fd,bytes = 0, length = 0;
    unsigned char buf[64] = "Hello,I'm testing the file system";
    unsigned char revBuf[64] = {0};
    /*fd = creat("/D/my.txt",O_RDWR); 
    if(fd == -1){
logMsg("error: creat file failed\r\n",0,0,0,0,0,0);
        return ERROR;
    }*/
    fd = open("/D/my.txt",O_RDWR,0);
    if(fd == -1){
logMsg("error: open file failed\r\n",0,0,0,0,0,0);
        return ERROR;
    }
    bytes = write(fd,buf,sizeof("Hello,I'm testing the file system"));
    logMsg("write %d bytes data\r\n",bytes,0,0,0,0,0);
    if(bytes != sizeof("Hello,I'm testing the file system")){
logMsg("error: write %d bytes data\r\n",bytes,0,0,0,0,0);
        close(fd);
        return ERROR;
    }
    close(fd);
    fd = open("/D/my.txt",O_RDWR,0);
    if(fd == -1){
logMsg("error: open file failed\r\n",0,0,0,0,0,0);
        return ERROR;
    }
    bytes = read(fd,revBuf,sizeof(revBuf));
    logMsg("read %d bytes data\r\n",bytes,0,0,0,0,0);
    revBuf[9] = '\0';   
    logMsg("read %d bytes data, and read date is %s\r\n",bytes,revBuf,0,0,0,0);
    close(fd);
    return OK;
}

实验总结:数组在初始化时必须指明分配的空间大小,不能用变量初始化。

不能write后立马去读数据,因为在写数据的时候文件指针已经指向了文件尾部,马上读的话什么也读不出来,需要使用lseek设置偏移指针才可正确读出。




原创粉丝点击