Solved

CFile::GetLength and CFile::Read

Posted on 2002-06-17
8
1,315 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
[X]
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
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
Learn how to optimize MySQL for your business need

With the increasing importance of apps & networks in both business & personal interconnections, perfor. has become one of the key metrics of successful communication. This ebook is a hands-on business-case-driven guide to understanding MySQL query parameter tuning & database perf

 

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

[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

Question has a verified solution.

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

Introduction: Database storage, where is the exe actually on the disc? Playing a game selected randomly (how to generate random numbers).  Error trapping with try..catch to help the code run even if something goes wrong. Continuing from the seve…
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.
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…

627 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