Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17


CFile::GetLength and CFile::Read

Posted on 2002-06-17
Medium Priority
Last Modified: 2013-11-20
Maybe a trivial question but I need to know this for sure once and for always. (so no trivial answers please)

I always see examples of reading a file's data by calling CFile::Read (or its Win32 equivalent or whatever equivalent) until there's no more data to read (the number of bytes read is less than the requested number of bytes). What's the reason for doing this?

Isn't it safe to just call CFile::GetLength and then CFile::Read with the requested length of the file?
Question by:searching
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
  • 2
  • +1
LVL 23

Expert Comment

by:Roshan Davis
ID: 7083377
The file read have the maximum buffer length, so if you are calling the Read/ReadFile with greater than that size, it will just return the data with length of maximum buffer length.

LVL 23

Expert Comment

by:Roshan Davis
ID: 7083392
Normally Buffer size is 4K,

So this is just for a safty.
If u have only less than that, u don't want to call ReadFile in a loop.

But for a generic program, that type of programming is peferable....

LVL 16

Expert Comment

ID: 7083711
2 roshmon
about which 4K buffer are you talking? CFile::Read don't  use internal buffer, AFAIK.

2 searching
>Isn't it safe to just call CFile::GetLength and then CFile::Read with the requested length of the file?
It can't be always safe. In some cases you can read nothing or only a part of requested data. So it is good style to check number of bytes read.
"Buffer problem" can be with a big files.
Will your db performance match your db growth?

In Percona’s white paper “Performance at Scale: Keeping Your Database on Its Toes,” we take a high-level approach to what you need to think about when planning for database scalability.


Author Comment

ID: 7083800
2 AlexNek

>It can't be always safe. In some cases you can read >nothing or only a part of requested data. So it
>is good style to check number of bytes read.

I can also check the number of bytes read using "CFile::Read(buffer, fileLength)" i.e. reading all of the file's data in one read operation.

>"Buffer problem" can be with a big files.
Do you mean a sort of buffer underrun problem? Is it likely that this problem will occur with big files?

Accepted Solution

peterchen092700 earned 300 total points
ID: 7084103
Some reasons:

* The buffer you use is smaller than the largest file you want to use (roshomon is prob. referring to this, although the 4K comes from something else)
* If you want to scan a huge file sequentially, it would be very ineffective to allocate a huge buffer (which is much more likely to fail), read it all at once, then scan it "in memory".
* If the file isn't opened exclusive, it could become longer or shorter beetween the GetLength() and the Read()
* Many things other than a File can hide behind a HANDLE (and so, to a lesser extent, behind a CFile)
* some implementations do intentially return "success" less bytes than specified were read. This might even be the cause for the same interface on different versions, or underlying storages
* block-by-block algorithms are generally more adaptable for different needs - e.g. if you want to switch to asynchronous read operations for better performance

* "It's the way we always did" ;-)


Expert Comment

ID: 7084130
The 4K buffer roshomon refers to is the standard cluster size on x86 platforms. This means: hard drive space is "allocated" to files in 4K units (so these 4K will always be sequential on disk). Virtual memory will be allocated in 4K chunks, and the memory manager will swap memory in 4K units (which is the actual reason for makingthe virtual memory chunks the same as the disk cluster size).

LVL 16

Expert Comment

ID: 7084464
2  searching
>I can also check the number of bytes read using...
Yes, you can do it in specifically sample but in general it is not a good. [Peterchen] give you some examples.

while not eof do
  read buffer
  use buffer

This strategy for sequential file reading is better, at least, you don't need to know the file size. You can work with file and not thinking how big it is.
But if you have fixed file size, 512 bytes by sample, it can't be be a good sence use buffer with 64 bytes.

>"Buffer problem"
It is possible that with you strategy you can't allocate a big enough buffer. For really big files can be better use File Mapping.

Author Comment

ID: 7091963
Tnx to all. I accepted the answer of peterchen. (Can't accept all of your comments ;-))

Featured Post

Survive A High-Traffic Event with Percona

Your application or website rely on your database to deliver information about products and services to your customers. You can’t afford to have your database lose performance, lose availability or become unresponsive – even for just a few minutes.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This is to be the first in a series of articles demonstrating the development of a complete windows based application using the MFC classes.  I’ll try to keep each article focused on one (or a couple) of the tasks that one may meet.   Introductio…
Introduction: Dynamic window placements and drawing on a form, simple usage of windows registry as a storage place for information. Continuing from the first article about sudoku.  There we have designed the application and put a lot of user int…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …

721 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question