当前位置: 技术问答>linux和unix
关于open
来源: 互联网 发布时间:2016-07-20
本文导语: 在linux下,有以下函数,open lseek read 都正常,但是读出来的全部问零,(文件不为零)是在windows下产生的二进制文件! ********************************************** * data_read * * input: addr: flash read address *...
在linux下,有以下函数,open lseek read 都正常,但是读出来的全部问零,(文件不为零)是在windows下产生的二进制文件!
**********************************************
* data_read
*
* input: addr: flash read address
* count: flash read count unit is byte
* buffer: flash read data buffer
*
* return: nagative int: please refer system call read
* 0 : init read failed
* others : read cout unit is byte
*
* ********************************************/
size_t data_read(void *addr, size_t count, void *buffer)
{
int fd;
off_t read_off;
if(!file2flash_inited)
{
file2flasherror("mtd not be mounted n");
return 0;
}
if(buffer == NULL)
{
file2flasherror("return buffer is NULL n");
return 0;
}
if( ((u_int32)addr = ( FLASH_START_ADDR + FLASH_SIZE) ) )
{
file2flasherror("flash address %p beyond flash region", addr);
return 0;
}
read_off = (unsigned int)(addr) - FLASH_START_ADDR;
fd = open(flashfilename, O_RDONLY , S_IRUSR);//| O_CREAT
if (fd != 0)
{
off_t offset;
ssize_t readlen;
offset = lseek(fd, read_off, SEEK_SET);
if(offset != read_off)
{
file2flasherror("read lseek to %d failed n", read_off);
close(fd);
return 0;
}
count = (count > (FLASH_SIZE - read_off)) ? (FLASH_SIZE - read_off) : count;
readlen = read(fd, buffer, count);
if (readlen >= 0)
/* some data has been read */
{
/* all no read buffer fill with 0xff */
memset((char *)buffer + readlen , 0xFF, count - readlen);
/* if read some data, always regard as read count bytes */
readlen = count;
}
close(fd);
return readlen;
}
else
{
file2flasherror("flash read open file %s failed n", flashfilename);
return 0;
}
} /* end of data read */
有谁能帮忙看看!
**********************************************
* data_read
*
* input: addr: flash read address
* count: flash read count unit is byte
* buffer: flash read data buffer
*
* return: nagative int: please refer system call read
* 0 : init read failed
* others : read cout unit is byte
*
* ********************************************/
size_t data_read(void *addr, size_t count, void *buffer)
{
int fd;
off_t read_off;
if(!file2flash_inited)
{
file2flasherror("mtd not be mounted n");
return 0;
}
if(buffer == NULL)
{
file2flasherror("return buffer is NULL n");
return 0;
}
if( ((u_int32)addr = ( FLASH_START_ADDR + FLASH_SIZE) ) )
{
file2flasherror("flash address %p beyond flash region", addr);
return 0;
}
read_off = (unsigned int)(addr) - FLASH_START_ADDR;
fd = open(flashfilename, O_RDONLY , S_IRUSR);//| O_CREAT
if (fd != 0)
{
off_t offset;
ssize_t readlen;
offset = lseek(fd, read_off, SEEK_SET);
if(offset != read_off)
{
file2flasherror("read lseek to %d failed n", read_off);
close(fd);
return 0;
}
count = (count > (FLASH_SIZE - read_off)) ? (FLASH_SIZE - read_off) : count;
readlen = read(fd, buffer, count);
if (readlen >= 0)
/* some data has been read */
{
/* all no read buffer fill with 0xff */
memset((char *)buffer + readlen , 0xFF, count - readlen);
/* if read some data, always regard as read count bytes */
readlen = count;
}
close(fd);
return readlen;
}
else
{
file2flasherror("flash read open file %s failed n", flashfilename);
return 0;
}
} /* end of data read */
有谁能帮忙看看!
|
文件格式不兼容,可能读出来的有问题吧。
|
跟编译器有关,这种直接写二进制的文件,在跨平台的时候经常出现问题。因为MS的编译器和gcc的编译器很可能
内部的机制不一样,比如对齐;这样在windows上生成的二进制文件,到linux下读取的时候就出问题了
建议你在linux下先写一个,然后再读一次试试看。如果没问题,那就可以确定是跨平台以及跨编译器引起的问
题了
内部的机制不一样,比如对齐;这样在windows上生成的二进制文件,到linux下读取的时候就出问题了
建议你在linux下先写一个,然后再读一次试试看。如果没问题,那就可以确定是跨平台以及跨编译器引起的问
题了
您可能感兴趣的文章:
本站(WWW.)旨在分享和传播互联网科技相关的资讯和技术,将尽最大努力为读者提供更好的信息聚合和浏览方式。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。