Thread View: alt.msdos.programmer
5 messages
5 total messages
Started by Gregary John Boyles
Wed, 10 Jan 1996 13:40
Some C problems
Author: Gregary John Boyles
Date: Wed, 10 Jan 1996 13:40
Date: Wed, 10 Jan 1996 13:40
85 lines
4187 bytes
4187 bytes
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
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);
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).
******************************************************************************
* *
* 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
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?
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?
Re: Some C problems
Author: Gene Wirchenko
Date: Thu, 11 Jan 1996 03:45
Date: Thu, 11 Jan 1996 03:45
48 lines
1792 bytes
1792 bytes
cs102238@lux.latrobe.edu.au (Gregary John Boyles ) wrote:
>PROBLEM 1.
[snip]
>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;
What are the types of the vars in the multiplication? If they
were (say) all int, there would be no type conversion. Overflow could
occur. This is probably your problem.
>df_total:clusters
>df_sclus:sectors/cluster
>df_bsec:bytes/sector
>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?
1) What is the value of the variable? If it contains a garbage
value, that's what you'll get. 2) Maybe your printf() is wrong: post
it.
>PROBLEM 4.
[snip]
Sincerely,
Gene Wirchenko
C Pronunciation Guide:
y=x++; "wye equals ex plus plus semicolon"
x=x++; "ex equals ex doublecross semicolon"
Re: Some C problems
Author: William Hutto
Date: Thu, 11 Jan 1996 23:37
Date: Thu, 11 Jan 1996 23:37
151 lines
5386 bytes
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?"
Re: Some C problems
Author: Rob McCafferty
Date: Fri, 12 Jan 1996 04:53
Date: Fri, 12 Jan 1996 04:53
25 lines
1431 bytes
1431 bytes
In <4d0fjj$eok@lugb.latrobe.edu.au>, cs102238@lux.latrobe.edu.au (Gregary John Boyles ) writes: > const word MaxBytes=65535; Produces warning : conversion may lose I believe C assumes numeric constants are signed. Try using 65535U. >DriveSize=DiskInfo.df_total*DiskInfo.df_sclus*DiskInfo.df_bsec; I'm assuming these DiskInfo fields are int's or unsigned int's? Cast the expression to long before assigning it: DriveSize=(long)DiskInfo.... >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? Are you 100% absolutely sure that ALL fields in your format string are of the correct type? >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? Again, C assumes constants are int unless you tell it otherwise. Make this 1000000L. --Rob
Re: Some C problems
Author: Tanmoy Bhattacharya
Date: Sat, 13 Jan 1996 07:06
Date: Sat, 13 Jan 1996 07:06
39 lines
2229 bytes
2229 bytes
--text follows this line-- In article <4d4pg7$3ot@news.dx.net> robmc@shol.com (Rob McCafferty) writes: <snip> I believe C assumes numeric constants are signed. Try using 65535U. Unsuffixed decimal constants are signed unless a long is insufficient to hold them, in which case they are unsigned. (If they don't fit into an unsigned long, it is an error). For octal and hexadecimal, they are signed; except if they fit into an unsigned int or unsigned long, but not into the corresponding signed type. <snip> >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? Again, C assumes constants are int unless you tell it otherwise. Make this 1000000L. An ANSI C compiler has to interpret 1000000 and 1000000L identically if 1000000 > MAX_INT (because it must fit into an unsigned int, there is no possibility of it being an error). ANSI C compilers assume unsufixed constants to be ints, _unless_ they do not fit into an int. In this case, long and unsigned long are tried. For octal and hexadecimal constants, unsigned int is tried before trying signed and unsigned longs. Cheers Tanmoy -- tanmoy@qcd.lanl.gov(128.165.23.46) DECNET: BETA::"tanmoy@lanl.gov"(1.218=1242) Tanmoy Bhattacharya O:T-8(MS B285)LANL,NM87545 H:#9,3000,Trinity Drive,NM87544 Others see <gopher://yaleinfo.yale.edu:7700/00/Internet-People/internet-mail>, <http://alpha.acast.nova.edu/cgi-bin/inmgq.pl>or<ftp://csd4.csd.uwm.edu/pub/ internetwork-mail-guide>. -- <http://nqcd.lanl.gov/people/tanmoy/tanmoy.html> fax: 1 (505) 665 3003 voice: 1 (505) 665 4733 [ Home: 1 (505) 662 5596 ]
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads