🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: alt.msdos.programmer
Article #1178

Re: Some C problems

#1178
From: William Hutto
Date: Thu, 11 Jan 1996 23:37
151 lines
5386 bytes
In article <4d0fjj$eok@lugb.latrobe.edu.au>,
   cs102238@lux.latrobe.edu.au (Gregary John Boyles ) wrote:
>PROBLEM 1.
>void WriteInFileToOutFile(FILE *InFile,FILE *OutFile,char *InFileName,char 
*OutFileName,long NumBytes,byte DiskNum)
>{
>     const word Size=1;
>     const word MaxBytes=65535; Produces warning : conversion may lose 
                                                    ^^^^^^^^^^

You better check this out. The compiler is telling you what it's doing. If 
necessary just change to:

const unsigned MaxBytes;

>                                significant bits. Why? 65535 is within the
>                                range for a word (unsigned int)
>
>     word NumRead,NumWritten;
>     long Index;
>     void *BufferPtr;
>
>     /* Write the disk number to the first byte of the file. */
>     fwrite(&DiskNum,Size,1,OutFile);
>     BufferPtr=(void *)malloc(MaxBytes);

On my machine, even with short test code, I wouldn't think of allocating 65535 
bytes without checking for success:

	if(BufferPtr==NULL)
		/*error*/

>     Index=0;
>     while (Index<=NumBytes)
>     {
>
>
>Am I using fread and fwrite correctly or do I need to dereference bufferptr 
>when I pass it to these procedures because I am finding that NumRead 
>(number of bytes read) != NumWritten (number of bytes written - 0).
<snip>

If you checked for success when opening these files the same way you did with 
the malloc() function above (no checking at all), I would imagine that OutFile 
either didn't open successfully or it wasn't opened in a write mode. Test the 
pointers that these functions (malloc() and fopen() [assuming you used 
fopen()]) return against NULL.


>
>*****************************************************************************
*
>*                                                                            
*
>*         NumRead=(fread(BufferPtr,Size,MaxBytes,InFile))*Size;              
*
>*         if (NumRead==0)                                                    
*
>*         {                                                                  
*
>*              strcpy(ErrorMessage,"An error occured while reading from 
infil*
>*              strcat(ErrorMessage,InFileName);                              
*
>*              strcat(ErrorMessage," - please make sure that this file is 
not*
>*              Error(ErrorMessage);                                          
*
>*              fclose(OutFile);                                              
*
>*              fclose(InFile);                                               
*
>*              exit(1);                                                      
*
>*         }                                                                  
*
>*         NumWritten=(fwrite(BufferPtr,Size,NumRead,OutFile))*Size;          
*
>*         if (NumWritten!=NumRead)                                           
*
>*         {                                                                  
*
>*              strcpy(ErrorMessage,"An error occured while writing to 
outfile*
>*              strcat(ErrorMessage,OutFileName);                             
*
>*              strcat(ErrorMessage," - number of bytes read does not equal 
th*
>*              Error(ErrorMessage);                                          
*
>*              fclose(OutFile);                                              
*
>*              fclose(InFile);                                               
*
>*              exit(1);                                                      
*
>*         }                                                                  
*
>*****************************************************************************
*
>
>          Index+=NumRead;
>     }
>}
>
>
>
>With this statement DiskInfo.df_total*DiskInfo.df_sclus*DiskInfo.df_bsec 
calculated
>using my calculator produces the correct drive capacity i.e. 360K however
>DriveSize does not contain the correct value i.e. 32000 instead of 360000 
(approximate).
>Why is this happening? The above fields are of type unsigned.
>
>PROBLEM 2.
>long DriveSize;
>struct dfree DiskInfo;
>..
>..
>..
>
>getdfree(DriveNum,&DiskInfo);
>DriveSize=DiskInfo.df_total*DiskInfo.df_sclus*DiskInfo.df_bsec;
>
>df_total:clusters
>df_sclus:sectors/cluster
>df_bsec:bytes/sector

This can be found in a good debugging session. You have to trace down where 
this value is coming from. 

>
>
>PROBLEM 3.
>When I want to output a long int type varaible with printf, it prints out
>a garbage value for the variable despite declaring it as a li/ld in the
>format string. What am I doing wrong?

Define a garbage value.

>
>PROBLEM 4.
>How do you use literal constants bigger than words e.g. how would you use
>the literal constant 1000000 without getting the 'constant out of range'
>warning/error?

1000000L

Bill

"Whatcha got on?...Your mind?"
Technical Details
Message-ID: <4d46v5$1vs0@news.gate.net>
Path: arc1-ca.pugleaf.net!gate.net!pslfl2-14
References: <4d0fjj$eok@lugb.latrobe.edu.au>

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!gate.net!pslfl2-14
From: bhutto@gate.net (William Hutto)
Newsgroups: alt.msdos.programmer,comp.lang.c
Subject: Re: Some C problems
Date: 11 Jan 1996 23:37:09 GMT
Organization: CyberGate, Inc.
Message-ID: <4d46v5$1vs0@news.gate.net>
References: <4d0fjj$eok@lugb.latrobe.edu.au>
NNTP-Posting-Host: pslfl2-14.gate.net
X-Newsreader: News Xpress Version 1.0 Beta #4