对SD进行读写测试

#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#define SDCARDDIR "/media/10-3M/rwfile-test.txt"
#define MAXSIZE 512
//#define MAXRW 1024*1024*10
#define MAXLOOP 15155
int main(int argc, char *argv[])
{
 int fd;
 if ((fd=open(SDCARDDIR, O_APPEND|O_RDWR|O_CREAT, 777)) < 0)
 {
  printf("cannot open this file\n");
  exit(0);
 }
 char buf[MAXSIZE] = {0};
 memset(buf, 'a', MAXSIZE);
 //int count = 0;
 //int countw = 0;//write SDcard block
 int loop = 0; //loop R-W 500次
 ssize_t ret = 0;
 //int sumloop = 0; //bad SDcard block
 int i;
 int ierror = 0;
 float per;
 while(1)
 {
  for(i = 0; i < MAXLOOP; i++)
  {
   ret = write(fd, buf, MAXSIZE);
   if(ret < 0)
   {
    ierror++;
    printf("iplace:%d, error:%d\n", i, errno);
    break;
   }
   //sync();
   
  }
  sync();
  loop += 1;
  if(ierror > 0)
  {
   per = (float)(ierror/MAXLOOP);
   printf("loop:%d, ierror:%d, bakblok-per:%f,error:%d\n", loop - 1, ierror, per,errno);
   ierror = 0;
  }
  //sumloop = ierror;
  //ierror = 0;
  if(loop > 500)
  {
   //printf("500 rw over loop %d, bad-block %d\n", loop, ierror);
   printf("500 rw over loop \n");
   break;
  }
  else
  {
   
   close(fd);
   remove(SDCARDDIR);
   sync();
   //fseek(fp, 0L, SEEK_SET); 
   if ((fd=open(SDCARDDIR, O_APPEND|O_RDWR|O_CREAT, 777)) < 0)
   {
    printf("cannot open this file\n");
    exit(0);
   }
  }
 }
 return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值