Question about the function "read" of the ifstream class

This is a discussion on Question about the function "read" of the ifstream class within the C++ Programming forums, part of the General Programming Boards category; Hello I have the following code: Code: ifstream file("file.data"); int number = 0; file.read((char *)&number, sizeof(int)); I would like to ...

  1. #1
    nik
    nik is offline
    Registered User
    Join Date
    Nov 2010
    Posts
    44

    Question about the function "read" of the ifstream class

    Hello I have the following code:

    Code:
    ifstream file("file.data");
    int number = 0;
    file.read((char *)&number, sizeof(int));
    I would like to understand what the third line does.

    the read function reads a block of 4 bytes from the file "file.data".

    the first argument specifies where we want to save that block of data.

    in what format is the data going to be saved in this variable?

    (char *) reminds me of an array of chars(bytes). So the block will be in bytes?

    and why is there an "&" after that? I tried without it but it didn't work.

    the & is the address of the variable "number"
    and then it takes this address and casts it to a sequence of bytes

    I don't really understand this

    thanks in advance

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    2,739
    the read function reads a block of 4 bytes from the file "file.data".
    Correct, if sizeof(int) is 4 bytes.
    in what format is the data going to be saved in this variable?
    Since the variable is of an int type, it will write the data to this int variable.
    (char *) reminds me of an array of chars(bytes). So the block will be in bytes?
    This is a C style cast. The read function reads a series of bytes from the file and places them into the variable whose address you provide.
    and why is there an "&" after that? I tried without it but it didn't work.
    You must send the address of the variable to read, so it can alter the contents of this variable.
    the & is the address of the variable "number"
    Yes.
    and then it takes this address and casts it to a sequence of bytes
    No, it is saying take these 4 bytes and put them into the location that starts at the address of "number".

    Jim

  3. #3
    nik
    nik is offline
    Registered User
    Join Date
    Nov 2010
    Posts
    44
    thanks a lot for the explanation

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. the operator "="error in class "String"
    By boyhailong in forum C++ Programming
    Replies: 9
    Last Post: 07-27-2011, 08:39 PM
  2. Replies: 21
    Last Post: 06-16-2008, 02:44 PM
  3. Replies: 8
    Last Post: 05-04-2008, 12:39 PM
  4. "itoa"-"_itoa" , "inp"-"_inp", Why some functions have "
    By L.O.K. in forum Windows Programming
    Replies: 5
    Last Post: 12-08-2002, 07:25 AM
  5. "CWnd"-"HWnd","CBitmap"-"HBitmap"...., What is mean by "
    By L.O.K. in forum Windows Programming
    Replies: 2
    Last Post: 12-04-2002, 06:59 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21