fread

fin.read((char*)&amt,sizeof(amt)


What does this line do?

(char*)&amt used for?Where can I study about this?
(char*)&amt used for?


(char*) is a cast to char pointer.

&amt takes the address of amt, which will be a type_of_amt pointer. For instance if amt is an int, &amt is basically int* -- which needs to be casted to char* because that's the type the function expects.

Where can I study about this?

http://www.cplusplus.com/doc/tutorial/typecasting/
http://www.cplusplus.com/reference/istream/istream/read/

Edit: typo and second link.
Last edited on
Catfish3]

Thanks for your help :)
Last edited on
fin.read((char*)&amt,sizeof(amt)


Does this code mean after get the sizeof(amt) and assign into char *amt?
Does this code mean after get the sizeof(amt) and assign into char *amt?

It takes the size in bytes of amt and requests that that number of characters are read from the file fin and stored in the block of memory starting at the address given by &amt.

http://www.cplusplus.com/reference/istream/istream/read/
It means to read n bytes into amt, where n == sizeof(amt).
Also, byte == char.

Suppose you read an int.

An int is usually composed of 4 bytes. So obviously you must read 4 bytes (aka chars) to read a full int.

So sizeof says how many bytes are needed for a full amt.
Then read() will read that number of bytes.
Topic archived. No new replies allowed.