请教各位大侠,如何直接读取u盘上的文件内容(非系统缓冲中的数据)?

1586874907 2012-06-06 11:27:25
我的代码如下:
int Handle;
WORD i;
BYTE TmpWBuf[8192];
BYTE TmpRBuf[8192];
int Tmp;
BYTE ch;

if(CreateDirectory("H:\\tmp", NULL))
{
printf("create folder ok\n");
}

if(-1 == (Handle = open("H:\\tmp\\test.txt", O_RDWR | O_CREAT | O_BINARY, 0x0180)))
{
printf("open file error\n");
}


for(i=0; i<512; i++)
{
TmpWBuf[i] = i;
}

write(Handle, TmpWBuf, 8192);

lseek(Handle, 0, SEEK_SET);
read(Handle, TmpRBuf, 2048);//该句并没有直接去读取U盘里的文件内容,而是缓存里的
close(Handle);


怎么做才能直接读取到u盘里的文件呢?请教各位
...全文
225 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
1586874907 2012-07-13
  • 打赏
  • 举报
回复
必须得加FILE_FLAG_NO_BUFFERING参数
1586874907 2012-07-13
  • 打赏
  • 举报
回复
昨天有空又看了下,发现之前设置的CreateFile参数有问题
结论:CreateFile()可以实现我的要求
非常感谢热心的zhao4zhong1,谢谢!
1586874907 2012-06-26
  • 打赏
  • 举报
回复
最后还是用扇区操作来做了,调用WriteSector()及ReadSector()函数来做了。
个人理解是open,read,write文件操作确实没有缓存了,但是usb的host controller模块有缓存的数据,包括fat,root dir及当前操作过的扇区内容等基本信息
赵4老师 2012-06-06
  • 打赏
  • 举报
回复
_fsopen
赵4老师 2012-06-06
  • 打赏
  • 举报
回复
试试看,不一定行。
CreateFile
HANDLE CreateFile(
LPCTSTR lpFileName, // pointer to name of the file
DWORD dwDesiredAccess, // access (read-write) mode
DWORD dwShareMode, // share mode
LPSECURITY_ATTRIBUTES lpSecurityAttributes,
// pointer to security attributes
DWORD dwCreationDisposition, // how to create
DWORD dwFlagsAndAttributes, // file attributes
HANDLE hTemplateFile // handle to file with attributes to
// copy
);

Parameters
...
dwFlagsAndAttributes
Specifies the file attributes and flags for the file.
Any combination of the following attributes is acceptable for the dwFlagsAndAttributes parameter, except all other file attributes override FILE_ATTRIBUTE_NORMAL.
Any combination of the following flags is acceptable for the dwFlagsAndAttributes parameter. Flag Meaning
FILE_FLAG_WRITE_THROUGH
Instructs the system to write through any intermediate cache and go directly to disk. The system can still cache write operations, but cannot lazily flush them.
FILE_FLAG_OVERLAPPED
Instructs the system to initialize the object, so that operations that take a significant amount of time to process return ERROR_IO_PENDING. When the operation is finished, the specified event is set to the signaled state.
When you specify FILE_FLAG_OVERLAPPED, the file read and write functions must specify an OVERLAPPED structure. That is, when FILE_FLAG_OVERLAPPED is specified, an application must perform overlapped reading and writing.
When FILE_FLAG_OVERLAPPED is specified, the system does not maintain the file pointer. The file position must be passed as part of the lpOverlapped parameter (pointing to an OVERLAPPED structure) to the file read and write functions.
This flag also enables more than one operation to be performed simultaneously with the handle (a simultaneous read and write operation, for example).
FILE_FLAG_NO_BUFFERING
Instructs the system to open the file with no intermediate buffering or caching. When combined with FILE_FLAG_OVERLAPPED, the flag gives maximum asynchronous performance, because the I/O does not rely on the synchronous operations of the memory manager. However, some I/O operations will take longer, because data is not being held in the cache.
An application must meet certain requirements when working with files opened with FILE_FLAG_NO_BUFFERING:

File access must begin at byte offsets within the file that are integer multiples of the volume's sector size.
File access must be for numbers of bytes that are integer multiples of the volume's sector size. For example, if the sector size is 512 bytes, an application can request reads and writes of 512, 1024, or 2048 bytes, but not of 335, 981, or 7171 bytes.
Buffer addresses for read and write operations must be sector aligned (aligned on addresses in memory that are integer multiples of the volume's sector size).
One way to align buffers on integer multiples of the volume sector size is to use VirtualAlloc to allocate the buffers. It allocates memory that is aligned on addresses that are integer multiples of the operating system's memory page size. Because both memory page and volume sector sizes are powers of 2, this memory is also aligned on addresses that are integer multiples of a volume's sector size.

An application can determine a volume's sector size by calling the GetDiskFreeSpace function.

1586874907 2012-06-06
  • 打赏
  • 举报
回复
有没有什么办法可以把缓存里的东西先清掉,然后再读或许会可以?
1586874907 2012-06-06
  • 打赏
  • 举报
回复
多谢各位
试了下_fsopen,还是不行;
我是在PC上,用vc,Linux下加这个O_DIRECT确实是可以的,这个之前倒是试过
wsxxiaohao 2012-06-06
  • 打赏
  • 举报
回复
不懂,学习学习。
沭水河畔 2012-06-06
  • 打赏
  • 举报
回复

Linux下加这个O_DIRECT open。

69,381

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧