最新消息:20190717 VPS服务器:Vultr新加坡,WordPress主题:大前端D8,统一介绍入口:关于

文件操作open read write 以及大于2GB的文件seek的问题

工作和技术 crifan 1346浏览 0评论

1.普通的小文件打开,就用正常的:

open, read, write,lseek,close即可。

2.经过测试,如果lseek大于2GB,那么就要用lseek64,对应地,open也改成open64

3.更大的文件的seek,尤其是大 > 4GB的,可以参考如下makefile以及C源码:

【makefile】

# Makefile
# version: 1.0
# Author: Crifan

#CC = arm-linux-uclibc-gcc
CC = arm-linux-gcc
DEFINES = __KERNEL__
#DEFINES =
CFLAGS= -D_FILE_OFFSET_BITS=64 -D_LARGEFILE64_SOURCE -D_LARGE_FILE
#CFLAGS= -D_LARGE_FILE
#CFLAGS=
SRC_FILE = as352x_lba_test.c
OUT_FILE = lba_test

all:
$(CC) -D$(DEFINES) $(CFLAGS) -o $(OUT_FILE) $(SRC_FILE)
clean:
rm -f $(patsubst %.c,%.o,$(filter %.c, $(SRC_FILE))) $(OUT_FILE)

【C源码】

#include <stdio.h>
#include <sys/types.h>
#include <fcntl.h>
#include <string.h>

#include "as352x_lba_test.h"

/* if lseek > 2GB, then should use lseek64 */

/* follow define is equal with include -D */
//#define   _FILE_OFFSET_BITS 64 /* == -D_LARGEFILE64_SOURCE while compile */
//#define _LARGEFILE64_SOURCE /* == -D_LARGEFILE64_SOURCE */

#include <unistd.h>

static int lba_file = 0;

/*******************************************************/
#define BUFFER_SIZE 1024

#define LINE_WIDTH 16

void lba_print_buf(unsigned char *buf, int len)
{
int idx;

printf("buffer=0x%p, len=%d :n", buf, len);
for(idx = 0; idx < len ; idx ++)
{
   printf( " %02x", buf[idx]);
   if(0==((idx+1)%(LINE_WIDTH/2)))
    printf( " ");
   if(0==((idx+1)%LINE_WIDTH))
    printf( "n");
}
printf( "n");
}

/*******************************************************/

int main(void)
{
unsigned long long pos;
unsigned char buffer[BUFFER_SIZE];
unsigned char pattern;

printf("Now will open ");
printf(AS352X_LBA_DEVICE_NAME);
printf(".n");

if(lba_file)
   close(lba_file);

lba_file = open64(AS352X_LBA_DEVICE_NAME, O_RDWR);

if(lba_file == -1)
{
   printf(AS352X_LBA_DEVICE_NAME);
   printf("open failed.n");
   return -1;
}
else
{
   printf("open OK: file =%d.n", lba_file);
}

#if 0
read(lba_file, buffer, BUFFER_SIZE);
lba_print_buf(buffer, BUFFER_SIZE);
#endif

//pos = (unsigned long long)0xE0000000; /* 3.5GB */
pos = (unsigned long long)0x10000 * 0x10000; /* 4GB */
// pos = 0x500000; /* 50M */
// pos = 0xA00000; /* 100M */

#if 0
lseek64(lba_file, pos, SEEK_SET);
read(lba_file, buffer, BUFFER_SIZE);
lba_print_buf(buffer, BUFFER_SIZE);
#endif

#if 1
/* write */
lseek64(lba_file, pos, SEEK_SET);
printf("…..seek to 0x%x…n", pos);
pattern = 0x35;
memset(buffer, pattern, BUFFER_SIZE);
printf("pattern = 0x%xn", pattern);
write(lba_file, buffer, BUFFER_SIZE);
printf("write done………………….n");
#endif

#if 1
/* read out */
lseek64(lba_file, pos, SEEK_SET);
printf("…..seek to 0x%x…n", pos);
read(lba_file, buffer, BUFFER_SIZE);
lba_print_buf(buffer, BUFFER_SIZE);
printf("read done………………….n");
#endif

close(lba_file);

printf("Now has close ");
printf(AS352X_LBA_DEVICE_NAME);
printf("……….n");

return 0;
}

转载请注明:在路上 » 文件操作open read write 以及大于2GB的文件seek的问题

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
53 queries in 0.099 seconds, using 18.84MB memory