Help specifying a specific size type in C++

This is a discussion on Help specifying a specific size type in C++ within the C++ Programming forums, part of the General Programming Boards category; One last question. Should I be using reinterpret_cast<char*>() for casting an int* to a char* because the compiler throws a ...

  1. #16
    Ethernal Noob
    Join Date
    Nov 2001
    Posts
    1,901
    One last question. Should I be using reinterpret_cast<char*>() for casting an int* to a char* because the compiler throws a fit if I use static_cast<char*>()

  2. #17
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    static_cast<>() would be better if it worked . . . but you probably shouldn't be casting an int* to a char* in the first place. Why do you need to do that?
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #18
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    ah, how dumb of me. But it's perfectly ok to use read for non string data?
    that's what it's there for. binary file io.

  4. #19
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Like this? Sure.
    Code:
    int array[5];
    file.write(static_cast<char *>(&array), sizeof(array));
    What exactly is your code?
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  5. #20
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >But it's perfectly ok to use read for non string data?
    If you're creating the file with a text editor of some kind, it makes more sense to use << or get() or getline() to read the data. You can also use std::string instead of a char array.
    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    using namespace std;
    
    typedef struct
    {
        string title;
        string type;
        int xpts;
        int ypts;
    } TerrainHeader;
    
    int main()
    {
        TerrainHeader ter;
        std::fstream inputfile("scape.ter", ios::in);
        getline(inputfile, title, ' ');  //Use space as delimiter (the default is a newline)
        //inputfile << title;  //Equivalent to the line above
        cout << ter.title << "\n";
        return 0;
    }

  6. #21
    Ethernal Noob
    Join Date
    Nov 2001
    Posts
    1,901
    Quote Originally Posted by dwks View Post
    static_cast<>() would be better if it worked . . . but you probably shouldn't be casting an int* to a char* in the first place. Why do you need to do that?
    Well If I want to read an integer value from the file into a data type I need to cast it to a char because that is what the fstream accepts. It's not like the fread that takes a void pointer. Unless there is a ifstream that takes a void pointer.

  7. #22
    Ethernal Noob
    Join Date
    Nov 2001
    Posts
    1,901
    OK I figured it out, c-style casting I guess is necessary for any of the fstream functions to work. Seems archaic but I guess that's how it is.

  8. #23
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,239
    Specifying specific specificities is a highly special and specialized specialty.

  9. #24
    Ethernal Noob
    Join Date
    Nov 2001
    Posts
    1,901
    specific shmeshmific.

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. char Handling, probably typical newbie stuff
    By Neolyth in forum C Programming
    Replies: 16
    Last Post: 06-21-2009, 04:05 AM
  2. failure to import external C libraries in C++ project
    By nocturna_gr in forum C++ Programming
    Replies: 3
    Last Post: 12-02-2007, 02:49 PM
  3. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  4. ras.h errors
    By Trent_Easton in forum Windows Programming
    Replies: 8
    Last Post: 07-15-2005, 10:52 PM
  5. Learning OpenGL
    By HQSneaker in forum C++ Programming
    Replies: 7
    Last Post: 08-06-2004, 08:57 AM

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