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*>()
Printable View
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?
that's what it's there for. binary file io.Quote:
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));
>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. :D