Solved

CFile::GetLength and CFile::Read

Posted on 2002-06-17
8
1,227 Views
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?
0
Comment
Question by:searching
  • 2
  • 2
  • 2
  • +1
8 Comments
 
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.

Roshmon
0
 
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....

Roshmon
0
 
LVL 16

Expert Comment

by:AlexNek
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.
0
 

Author Comment

by:searching
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?
0
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
LVL 7

Accepted Solution

by:
peterchen092700 earned 100 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" ;-)

0
 
LVL 7

Expert Comment

by:peterchen092700
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).

0
 
LVL 16

Expert Comment

by:AlexNek
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
end

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.
0
 

Author Comment

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

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Replacement selected text 2 47
centeredAverage challenge 8 121
fix34  challenge 9 96
mapAB Challlenge 35 85
Introduction: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
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.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

707 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now