Re: question



"Bill Cunningham" <nospam@xxxxxxxxx> writes:
I wrote this small program to read a 512 block of binary data and write
the same to a file. My code compiled well. The only thing is when I ran the
compilers binary instead of a data file of 512 bytes I got one of 2048
bytes.

#include <stdio.h>

main(){

"int main(void) {"

int buf[512];
FILE *fp;
fp=fopen("r.dsk","rb");
if (fp==NULL) {printf("Error"); exit(0);}

You need "#include <stdlib.h>" for exit.

fread(buf,sizeof(int),512,fp);

No error checking.

fclose(fp);

No error checking (yes, fclose can fail).

fp=fopen("dat","wb");
if (fp==NULL) {printf("Error");}

Above, you printed an error message and terminated the program. Here
you print an error message and continue.

fwrite(buf,sizeof(int),512,fp);

No error checking.

fclose(fp);}

No error checking (yes, fclose can fail).

Add "return 0;".

*Please* put the closing "}" on a line by itself. It's very difficult
to see.

Is it the code or some overhead from the compiler or linker?

And finally, the answer to your question:

The program is doing exactly what it's supposed to do. Read the
documentation for fread() and fwrite(). They both take two size_t
arguments, the size in bytes of each element and the number of
elements. You're asking fread() to read 512 element, each of which is
sizeof(int) bytes (in other words, 512 ints, not 512 bytes). If int
is 4 bytes on your system, you'll read and write 2048 bytes (assuming
there are no errors).

One more thing: it's conventional to print error messages to stderr,
and to use the argument to exit() to indicate success or failure.
Rather than

if (fp==NULL) {printf("Error"); exit(0);}

I'd write:

if (fp == NULL) {
fprintf(stderr, "Error\n");
exit(EXIT_FAILURE);
}

Yes, I also changed the code layout. Whitespace is not in short
supply; use as much as you need to make the code clear and readable.

--
Keith Thompson (The_Other_Keith) <kst-u@xxxxxxx>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
.



Relevant Pages

  • Re: question
    ... "int main{" ... you printed an error message and terminated the program. ... You're asking fread() to read 512 element, ... it's conventional to print error messages to stderr, ...
    (comp.lang.c)
  • Re: Passing NULL Objects
    ... long accountNumber){int accNum=0; ... public Integer searchAccounts(Vector bankAccounts) { ... int value, which is a primitive, only to an object. ... lookup, I'd pass back a false in the boolean, followed by the error message ...
    (comp.lang.java.help)
  • Re: operator function
    ... >> of them have anything to do with what the error message seems ... Setting (int h, int m, int s, int TT) ... Now the compiler knows, that it is safe to use that function on a const ... >> operator returns a Setting object. ...
    (alt.comp.lang.learn.c-cpp)
  • Re: Using MessageBox
    ... Here is the error message. ... String* caption, unsigned int type); ... MClass* pm = new MClass; ... add System.Windows.Forms to your namespace list (I've never used ...
    (microsoft.public.dotnet.framework.clr)
  • Re: native dll access error - Attempted to read or write protected memory
    ... I was getting the last error message ... But I don't get anything back from the native function. ... int status = getPwd(chArrServer, chArrUser, ref chArrPwd); ...
    (microsoft.public.dotnet.languages.csharp)