Can someone point me in the right direction.
I want to open a file of a very specific size.
from there I want to assign pointers to various bytes in memory
I believe the best method of organizing them would be to create a data structure of pointers. The problem is I'm not very good with opening files. Most of my minimal experience reading from a file is through getline(). is there a way I can parse a file and directly access memory addresses of that file.
for instance, if I knew bytes 10 through 13 was a 32 bit number, it would be nice if I could cast a long pointer to 0x0A.
You can't cast a long pointer to an absolute offset, but you can create a pointer to the base address of the buffer + 10 bytes.
1 2 3 4 5 6 7 8 9 10 11
// Open the file in binary (not text) mode.
ifstream datafile ("filename", ios::binary);
// position to eof
// get the current position (eof)
// Reposition to the start of the file
char * buf = newchar[eof]; // Allocate a buffer for the file
datafile.read (buf, eof); // Read the file into the buffer
long * ptr = (long *)(buf + 10); // Point to a long starting at the 10th byte of the buffer
cout << "long at offset 10 = " << *ptr << endl;
Thanks for you help. That sounds like exactly what I wanted to do. I don't quite understand the last part but I am aware of longs being inturpreted as both 32 and 64 bit. If what you proposed at the bottom can remedy this, I'll look into it.
As for endianness issues, I generally don't deal with big endian architectures. When it comes to that I'll figure it out.
The function returns a pointer to a 32 bit value in your buffer. The function accepts the address of the buffer holding the file and the desired offset into the buffer (e.g. 10). It then performs the needed address arithmetic and returns you a pointer to the data you wanted. Using the function tends to be a lot clearer that doing the address arithmetic every time.
You would call it as follows:
1 2 3 4
int32_t * pdata; // pointer to some 32 bit data item in the file
pdata = get_int32ptr (buf, 10); // where buf is the memory buffer for the file
cout << "32 bit value = " << *pdata << endl;