fread() in fstream?

This is a discussion on fread() in fstream? within the C++ Programming forums, part of the General Programming Boards category; Hi, Is there any function in <fstream> that reads n bytes from a file into a variable (of any type ...

  1. #1
    Registered User MathFan's Avatar
    Join Date
    Apr 2002
    Posts
    190

    fread() in fstream?

    Hi,

    Is there any function in <fstream> that reads n bytes from a file into a variable (of any type like int, char, etc.). I mean something like fread() in C. I found several functions in my book (like read() or readsome()), but they all read in a buffer of char*. What do I do if I want to read in an int?

    Thanx...
    The OS requirements were Windows Vista Ultimate or better, so we used Linux.

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,802
    Quote Originally Posted by MathFan
    Hi,

    Is there any function in <fstream> that reads n bytes from a file into a variable (of any type like int, char, etc.). I mean something like fread() in C. I found several functions in my book (like read() or readsome()), but they all read in a buffer of char*. What do I do if I want to read in an int?

    Thanx...
    Well, like using fread you can always read data into a memory area occupied by an int through the use of a cast.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903
    my bad.. you already know about those..

    They will read in anything (char will accept any character) then in your program if you need the data to be of type int.. just peform a typecast
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

  4. #4
    Registered User MathFan's Avatar
    Join Date
    Apr 2002
    Posts
    190
    But if a char is 8 bits and int is 16 bits, how can I convert one into another?
    The OS requirements were Windows Vista Ultimate or better, so we used Linux.

  5. #5
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903
    one of the joys of c++ is that the compiler will take care of the memory managment for ye'. feel free to typecast to anything you want basically. it's all good. (as long as you are typecasting to a larger container.. going in reverse may lead to truncation)
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

  6. #6
    Registered User MathFan's Avatar
    Join Date
    Apr 2002
    Posts
    190
    OK, thanx... heh, I think I must brush up some C++ basics But, isn't it a rather strange way of doing it? First reading something into a character, then casting it? Wouldn't it be easier to implement read(void*, int) in iostream?
    The OS requirements were Windows Vista Ultimate or better, so we used Linux.

  7. #7
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903
    I think using char (or string) is probably the best way to handle program input. char or string will accept anything.. unlike int, float, double, short, long which will literally go nuts if you try to input something that is not of that type.. example: if you tried to input "akj:LKJ:LKAH" into a type float.. your program would go bezerk. So, accept input using char or string.. typecast as necessary.. and successfully handle any invalid input.

    Here also is a recent post on error checking
    Last edited by The Brain; 06-14-2005 at 10:07 AM.
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

  8. #8
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    Whooooooah guys, I think we're heading in TOTALLY the wrong direction. Do NOT read into a character and then typecast to an int.

    What hk_mp5kdw meant is:
    Code:
    int myInt;
    file.read((char*)&myInt, sizeof(int));
    You take the address of the int and interpret it as char*, and read sizeof(int) bytes into the block of memory beginning at that address.

    I may be wrong about this, but I have the impression that you can't perform pointer arithmetic on void*, i.e. thePointer++. As such, I suspect the void* may be internally typecasted to char* or unsigned char* anyway in fread(). Anyway, since char and unsigned char are supposed to be 1 byte, they're convenient types for bytewise operations such as binary-mode file i/o with arbitrarily sized blocks of data - if you want to write a block of data to a file, the function doesn't care whether it's a char or int or MyBestFriend object; all it cares is that you pass it the location of the object (i.e. its address), and its size, and it will then write the information byte by byte into a file.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. fread problems or memory problems
    By Lechuza in forum C Programming
    Replies: 1
    Last Post: 03-22-2009, 12:45 PM
  2. Fstream. I/O
    By kevinawad in forum C++ Programming
    Replies: 2
    Last Post: 07-07-2008, 09:19 AM
  3. Why is fread sometimes taking so long?
    By manugarciac in forum C++ Programming
    Replies: 2
    Last Post: 04-28-2007, 11:25 PM
  4. ARGH! fstream errors
    By OttoDestruct in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2004, 10:37 PM
  5. Problems with fstreams.
    By mosdef in forum C++ Programming
    Replies: 7
    Last Post: 06-19-2002, 03:36 PM

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