[alt.msdos.programmer] Problem with disk full in programs compiled with Turbo-C

emigh@ncsugn.ncsu.edu (Ted H. Emigh) (03/27/90)

I am using Turbo-C (Version 2.0), and am having trouble determining if the
disk is full.  I have tried the obvious (at least to me obvious):
1)	Code fragment:
	result=fprintf(...)
	if(result==EOF) { do something}

	result always returns the number of characters -- even if the disk
	is full.  There is disk activity, but nothing is written (obviously).

2)	Code fragment:
	harderr(hardware_handler);
	...
	int hardware_handler() { do something }

	This routine is never called for disk full.  It functions OK for write-
	protect and disk not ready, but not for disk full.

Do you have any suggestions to help detect disk full?  Thanks.

PS.  What, really, is the difference between comp.sys.ibm.pc.programmer and
alt.msdos.programmer?

-- 
Ted H. Emigh, Dept. Genetics and Statistics, NCSU, Raleigh, NC
uucp:	mcnc!ncsuvx!ncsugn!emigh	internet:  emigh@ncsugn.ncsu.edu
BITNET: emigh%ncsugn@MCNC.UUCP  or  emigh%ncsugn@ncsuvx.ncsu.edu

mee@ucscb.UCSC.EDU (Kevin Kahl) (03/27/90)

In article <5705@ncsugn.ncsu.edu> emigh@ncsugn.UUCP (Ted H. Emigh) writes:
>I am using Turbo-C (Version 2.0), and am having trouble determining if the
>disk is full.  I have tried the obvious (at least to me obvious):
>1)	Code fragment:
>	result=fprintf(...)
>	if(result==EOF) { do something}
>
>	result always returns the number of characters -- even if the disk
>	is full.  There is disk activity, but nothing is written (obviously).

Indeed!  The result returned always is the number of bytes you attempted
to write.  This is probably a bug.  Here is a workaround:

	fprintf(...);
	result = ferror(filehandle);
	if (result != 0) { do error handling}

The ferror() routine returns 16 when the disk is full on my system.
Hope this helps!

-Kevin Kahl
 mee@ucscb.ucsc.edu

Ralf.Brown@B.GP.CS.CMU.EDU (03/27/90)

In article <5705@ncsugn.ncsu.edu>, emigh@ncsugn.ncsu.edu (Ted H. Emigh) wrote:
}I am using Turbo-C (Version 2.0), and am having trouble determining if the
}disk is full.  I have tried the obvious (at least to me obvious):
}1)      Code fragment:
}        result=fprintf(...)
}        if(result==EOF) { do something}
}
}        result always returns the number of characters -- even if the disk
}        is full.  There is disk activity, but nothing is written (obviously).
}
}2)      Code fragment:
}        harderr(hardware_handler);
}
}        This routine is never called for disk full.  It functions OK for write-
}        protect and disk not ready, but not for disk full.

Try
       fprintf(file,...) ;
       if (ferror(file))
	  { do something }

--
UUCP: {ucbvax,harvard}!cs.cmu.edu!ralf -=- 412-268-3053 (school) -=- FAX: ask
ARPA: ralf@cs.cmu.edu  BIT: ralf%cs.cmu.edu@CMUCCVMA  FIDO: Ralf Brown 1:129/46
"How to Prove It" by Dana Angluin              Disclaimer? I claimed something?
16. proof by cosmology:
    The negation of the proposition is unimaginable or meaningless.  Popular
    for proofs of the existence of God.

cs4g6ag@maccs.dcss.mcmaster.ca (Stephen M. Dunn) (06/02/90)

In article <22661.2612e98b@kuhub.cc.ukans.edu> 2fjomummer@kuhub.cc.ukans.edu writes:
$Although I've never actually tried it, there is an MS-DOS function that reports
$the free space on a drive.  I'm basically a Turbo Pascal programmer, so I don't

   Yes, this will work to a certain extent, but the granularity involved is
the cluster size.  On a hard disk with 8 sectors per cluster, for example,
this call will return 0 bytes free for any amount less than 4096 bytes.  If
I have 4K left on the disk and I want to write a 3K file, 1K at a time,
the first write will work, but after that it will report 0 free space, even
though I still have 3K left and only need 2K of it.

   There's no need to go into details of the ferror () solution, as many
people have posted it already.
-- 
               More half-baked ideas from the oven of:
****************************************************************************
Stephen M. Dunn                               cs4g6ag@maccs.dcss.mcmaster.ca
     <std_disclaimer.h> = "\nI'm only an undergraduate ... for now!\n";

2fjomummer@kuhub.cc.ukans.edu (06/02/90)

Article-I.D.: kuhub.22661.2612e98b
References: <5705@ncsugn.ncsu.edu>
Organization: University of Kansas Academic Computing Services
Lines: 38

In article <5705@ncsugn.ncsu.edu>, emigh@ncsugn.ncsu.edu (Ted H. Emigh) writes:
> I am using Turbo-C (Version 2.0), and am having trouble determining if the
> disk is full.  I have tried the obvious (at least to me obvious):
> 1)	Code fragment:
> 	result=fprintf(...)
> 	if(result==EOF) { do something}
> 
> 	result always returns the number of characters -- even if the disk
> 	is full.  There is disk activity, but nothing is written (obviously).
> 
> 2)	Code fragment:
> 	harderr(hardware_handler);
> 	...
> 	int hardware_handler() { do something }
> 
> 	This routine is never called for disk full.  It functions OK for write-
> 	protect and disk not ready, but not for disk full.
> 
> Do you have any suggestions to help detect disk full?  Thanks.

Although I've never actually tried it, there is an MS-DOS function that reports
the free space on a drive.  I'm basically a Turbo Pascal programmer, so I don't
know the exact commands for C, but the function you want to call is 36h.  You
put 36h in AH and the drive code in the DL register (use 0 for the default
drive, 1 for drive A:, 2 for drive B:, etc.).  If the drive code that
was given is bad, AX will be set to FFFFh, otherwise Dos will return the
following:
	AX = sectors per cluster
	BX = available cluster count
	CX = bytes per sector
	DX = total clusters on the disk
So, the total number of free bytes is simply AX * BX * CX.

If you call this before you perform the write, you should be able to tell if
you have enough free space on the disk.

By the way, I must give credit where it is due.  I got most of the above from
Peter Norton's  _Programmer's Guide to the IBM PC_.  Good book.

dmt@pegasus.ATT.COM (Dave Tutelman) (06/03/90)

In article <22661.2612e98b@kuhub.cc.ukans.edu> 2fjomummer@kuhub.cc.ukans.edu writes:
>In article <5705@ncsugn.ncsu.edu>, emigh@ncsugn.ncsu.edu (Ted H. Emigh) writes:
>> I am using Turbo-C (Version 2.0), and am having trouble determining if the
>> disk is full.  I have tried the obvious (at least to me obvious):
>> 1)	... [ return the result of a printf ] ....

MSDOS I/O to disk is buffered.  If DOS can write to the buffer, it says
OK, only to find out later that it can't flush the buffer to the disk.

>Although I've never actually tried it, there is an MS-DOS function that reports
>the free space on a drive.  
>...
>	AX = sectors per cluster
>	BX = available cluster count
>	CX = bytes per sector
>So, the total number of free bytes is simply AX * BX * CX.

Warning!!!!  The result will be a LONG, not an INT.  If you do your
multiplication in the "wrong" order, partial results may also be long.
The safest way to do this is to call the function, explicitly convert
all three results to long, then multiply into a long.

Failure to do this can result in strange answers.  And don't assume
that you "got away with it" just because you get right answers on
your setup.  I experienced problems with this on a program that had
worked for years, then started failing ONLY on MSDOS 4.0 with large
unpartitioned disks.

Cheers.
Dave
+---------------------------------------------------------------+
|    Dave Tutelman						|
|    Physical - AT&T Bell Labs  -  Lincroft, NJ			|
|    Logical -  ...att!pegasus!dmt				|
|    Audible -  (201) 576 2194					|
+---------------------------------------------------------------+