Solved

CFile::GetLength and CFile::Read

Posted on 2002-06-17
8
1,267 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
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
ORA-01403: no data found 43 106
List out all word 7 320
has77  challenge 9 93
haveThree challenge 22 122
Introduction: Dialogs (1) modal - maintaining the database. Continuing from the ninth article about sudoku.   You might have heard of modal and modeless dialogs.  Here with this Sudoku application will we use one of each type: a modal dialog …
If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
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.
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

807 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