[Tutor] Questions on file.read
alan.gauld at freenet.co.uk
Thu Jul 14 17:58:36 CEST 2005
> Notice that when in non-blocking mode, less data than what was
> may be returned, even if no size parameter was given.
> What does "blocking-mode" mean,
Its easier to answer by explaining blocking mode.
In blocking mode the read will wait(block) until size bytes are
available. This if the file were a log file being filled by another
process it would wait until the pther proicess had output enough
text to fulfill your read requirement.
In non blocking mode it will just return as much data as is
available when you make the request. 99% of the time non blocking
is what you will be, and what you want!
> reading a file I'm not in such modality (and eventually switch)?
> I need to read the whole content of a file, not just some pieces.
Just use read() without a size, you should be fine.
> numeric argument. When size is omitted or negative, the entire
> contents of the file will be read and returned; it's your problem
> if the file is twice as large as your machine's memory."
> What does exactly mean that it's my problem
It means you have to be sure it won't happen. If it does happen
you need to know what the consequences will be because it will
be different for every platform. On DOS or CP/M for example
the whole PC will crash, On Windows 3/95 the program will likely
crash when it exceeds 64K, in Unix/Win2K etc virtual memory may
be invoked until either you reach your disk quota(*) whereupon your
program will crash - if quotas have been set - or you run out
of disk in which case the machine will likely crash...
Im a mainframe it will run until you consume the memory you
said you needed in the JCL script that ran your program, then
the job will abbend, writing copious log files.
> How can my recognize a "too big file" before read it?
If you know how much RAM you want to consume you could use
the OS stat functions to find out the size of the file first.
(*)Every Unix production box should have quotas enabled at a sane
level if you really want the server to be reliable. One of the
most common causes of Unix box failure is due to virtual memory
filling disks to the point where Unix itself can't run!
Author of the Learn to Program web tutor
More information about the Tutor