荔园在线

荔园之美,在春之萌芽,在夏之绽放,在秋之收获,在冬之沉淀

[回到开始] [上一篇][下一篇]


发信人: huhaiming (一生只爱她), 信区: Program
标  题: [合集]请教:VC6怎样以非缓冲的方式读写文件啊?
发信站: 荔园晨风BBS站 (2004年10月30日08:58:27 星期六), 站内信件

☆   1  ──────────── 我是分割线 ─────────────────☆
发信人: jjksam (ramdisk_size=32768), 信区: Program
标  题: Re: 请教:VC6怎样以非缓冲的方式读写文件啊?
时  间: Sun May  2 19:03:45 2004

你用什么方法读的?

如果用Standard I/O Library的函数, 可以用
FILE *fp;
fp = fopen("test.txt", "r");
if(fp == NULL)
{
  printf("can't open file test.txt\n");
  exit(0);
}
setbuf(fd,NULL);
接着就可以使用无缓存的IO了.

把缓存置为0, 即unbuffered
或者直接用read(), write().




☆   2  ──────────── 我是分割线 ─────────────────☆
发信人: idiot (234!), 信区: Program
标  题: Re: 请教:VC6怎样以非缓冲的方式读写文件啊?
时  间: Sun May  2 23:08:05 2004

接着怎样啊?我在setbuf以后用fread还是没有直接读设备,
而是直接在系统缓存里取数据啊。我的意思是第一次read
时,系统向设备发送一个read命令,第二次read相同的内
容的时候,还要向设备再发一个read命令,尽管系统缓存
里已经有这部分内容,可以做到的吗?

另外read()这个函数怎样用的啊?我在msdn里找不到这个
函数啊,在那个头文件里定义的?谢谢。




☆   3  ──────────── 我是分割线 ─────────────────☆
发信人: jjksam (ramdisk_size=32768), 信区: Program
标  题: Re: 请教:VC6怎样以非缓冲的方式读写文件啊?
时  间: Mon May  3 11:20:01 2004

VC里面是
#include <io.h>

int _read(
   int fd,
   void *buffer,
   unsigned int count
);

Parameters
fd
File descriptor referring to open file.
buffer
Storage location for data.
count
Maximum number of bytes.

注意,如果你读写的是串口的话,串口设备里面也是会有缓存的,并不是
操作系统里面的缓存。

你的设备是什么?你具体是怎么读的呢?如果可以最好给一段代码。并给出
测试方法和结果。



☆   4  ──────────── 我是分割线 ─────────────────☆
发信人: idiot (234!), 信区: Program
标  题: Re: 请教:VC6怎样以非缓冲的方式读写文件啊?
时  间: Mon May  3 14:49:29 2004

我的设备就是磁盘、光盘里的文件,代码如下:

        int File;

        //m_PathName是文件名
        if( (File = _open(m_PathName, _O_RDONLY )) == -1 )
        {
                MessageBox("Open file fail!");
                return;
        }

        unsigned char cFileBuf[512];
        _read(File,cFileBuf,512);
        _close(File);

然后用这段程序读取CDROM里的一个文件,发现第一次读的时候,总线
上有数据传输,即系统向设备发了一个读命令。这个时候我让这段程序
再跑一次,再读同样的地方,总线上就没有数据传输了。证明系统直接
把上次的缓存传给了我的程序,而没有实际去设备取数据。至于总线上
的数据,我是通过软件抓下来的,可以保证系统没有向设备发读命令。

现在我就是想就算我读的是同一个地方,也希望系统可以发命令给设备,
把实际的数据回给我。我记得好像在打开文件的时候采用CreateFile()
函数有一个地方可以指定一个参数,通过这个参数就可以在读的时候实现
非缓存方式读写,但我没查到,不知道有没有记错。




☆   5  ──────────── 我是分割线 ─────────────────☆
发信人: jjksam (ramdisk_size=32768), 信区: Program
标  题: Re: 请教:VC6怎样以非缓冲的方式读写文件啊?
时  间: Mon May  3 15:23:30 2004

MSDN January 2004
-------------------------------------
HANDLE CreateFile(
  LPCTSTR lpFileName,
  DWORD dwDesiredAccess,
  DWORD dwShareMode,
  LPSECURITY_ATTRIBUTES lpSecurityAttributes,
  DWORD dwCreationDisposition,
  DWORD dwFlagsAndAttributes,
  HANDLE hTemplateFile
);

在dwFlagsAndAttributes参数中

用FILE_FLAG_NO_BUFFERING

注意下面问题:
The system is to open the file with no system caching. This flag has
no effect on hard disk 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. Also, the file metadata may still be cached. To flush
the metadata to disk, use the FlushFileBuffers function.
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 should be sector
aligned (aligned on addresses in memory that are integer multiples of
the volume's sector size). Depending on the disk, this requirement may
not be enforced.

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.




☆   6  ──────────── 我是分割线 ─────────────────☆
发信人: BrightWorld (Hello), 信区: Program
标  题: Re: 请教:VC6怎样以非缓冲的方式读写文件啊?
时  间: Wed May  5 09:47:56 2004

   VC 跟 VStudio是不是一个东东啊




☆   7  ──────────── 我是分割线 ─────────────────☆
发信人: Kenniel (Goal:HongKong Poly), 信区: Program
标  题: Re: 请教:VC6怎样以非缓冲的方式读写文件啊?
时  间: Wed May  5 09:54:46 2004

VStudio include vc vb vfp interdev...


[回到开始] [上一篇][下一篇]

荔园在线首页 友情链接:深圳大学 深大招生 荔园晨风BBS S-Term软件 网络书店