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*>()
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*>()
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.
that's what it's there for. binary file io.ah, how dumb of me. But it's perfectly ok to use read for non string data?
Like this? Sure.
What exactly is your code?Code:int array[5]; file.write(static_cast<char *>(&array), sizeof(array));
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.
>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; }
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.
Specifying specific specificities is a highly special and specialized specialty.
specific shmeshmific.