I Don't Get Part Of This Fstream Code?

So I was looking for tutorials on writing files i.e. filestreaming files and this works, and most of this I understand. Again though when it comes to pointers ...nope I don't get it. So in english maybe someone can explain this. So an unsigned character i.e. only positive numbers will be used obviously is created. outputting the data out and binary that all makes sense

Look at code and see my question below:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <fstream>
#include <iomanip>
#include <iostream>
#include <cstdlib>

using namespace std;
int main(int argc, char **argv[])
{

//binary data goes here
unsigned char image[] = {0x4d, 0x5a, 0x90, 0x0, 0x3, 0x0, 0x0, 0x0, 0x4, 0x0, 0x0, 0x0, 0xff, 0xff,};

        std::fstream myFile;              

        myFile.open ("hey.exe", std::ios::out | std::ios::binary);  
                                                                    

      if(myFile.is_open())

                 myFile.write((char*)image, sizeof(image));   

	  return 0;
                                                                    
}


myFile.write((char*)image, sizeof(image));

So when we get to that part I'm not getting ((char*) in parentheses like that, what does that do? I'm assuming it allocates the memory for the size of the array image and writes it into hey.exe.

I'm not getting the programming meaning of it though. Also I've seen this for executing binary data in an array such as like

1
2
3
4
char arr[] = "\x33\xc9\x83\xe9\x81\xe8\xff\xff\xff\xff\xc0\x5e\x81\x76\x0e"
int (*func)();
 func = (int (*)()) arr;
(int)(*func)();


So that said explain this this is way beyond basic pointer stuff. Especially explain the whose ((char*) for writing a file? Also what does int (*func)(); mean for example?

Thanks in advance.
 
myFile.write((char*)image, sizeof(image));


It's called casting. It is casting it from a statically declared char array to a char pointer.

In the old days of ANSI C, I would have to cast a memory block allocated with malloc to proper format because malloc returns a void pointer which has no structure.

In your case, the write member function of the iostream requires a const char *. Giving it an array is not sufficient so you cast it to a char *.

int (*func)();

That's a pointer to a function. It can be any "generic" function that takes no arguments and returns an int. Function names are little more than symbolic names for address spaces that mark the start of a block of execution, so you can supply that address by assigning a function name like you would any variable.
Last edited on
Thanks roberts that makes a little more sense now. Casting I've used many times before for simple data type conversions but I didn't really know it could be casted to a pointer char or pointer variable...
You normally would not:

int i;

(int *)i;

but I've had to do such things as:

memset( (void *)&vector_coord, 0, sizeof(vector_coord));

Because vector_coord is a statically declared struct of floats/doubles and memset takes a void *, so I pass the address of the struct and cast it as a void *.
Topic archived. No new replies allowed.