Homework 4 Intermediate c++

The Partridge Family were neither partridges nor a family. Discuss.
Post Reply
Anarkus
Posts: 7
Joined: February 27th, 2020, 4:03 am

Homework 4 Intermediate c++

Post by Anarkus » March 11th, 2020, 5:54 am

I'm having a really hard time wrapping my head around what is actually going on here. Any help would be greatly appreciated.

Code: Select all

		void Load(const char* filename) {
			std::ifstream in(filename, std::ios::binary);
			in.read(reinterpret_cast<char*>(&curNumberEntries), sizeof(curNumberEntries));
			for (int i = 0; i < curNumberEntries; i++) {
				entries[i].Deserialize(in);
			}
		}
		void Save(const char* filename)const {
			std::ofstream out(filename, std::ios::binary);
			out.write(reinterpret_cast<const char*> (&curNumberEntries), sizeof(curNumberEntries));
			for (int i = 0; i < curNumberEntries; i++) {
				entries[i].Serialize(out);
			}
I understand the basic idea of what's going on but I can't seem to visualize step by step what's actually happening. A few questions:
1. My understanding is that .write writes a chunk of data into the file starting at the address of curNumberentries with the size of curNumberEntries. Is this correct?
2. When you call .write/read does it move a pointer by the amount specified in sizeof?
3. Is this thing just writing a bunch of chars starting at the address specified into the file until it hits the limit specified by sizeof?
4. How are things organized in the file? Is it just one line of random data?
5. Why does this all seem like sorcery to me?

albinopapa
Posts: 4373
Joined: February 28th, 2013, 3:23 am
Location: Oklahoma, United States

Re: Homework 4 Intermediate c++

Post by albinopapa » March 11th, 2020, 4:46 pm

  1. Not exactly, .write() in this case is writing the bit pattern of curNumberentries that is the size of the variable curNumberentries type. If curNumberentries is an "int" and it's value is 10, the bit pattern would be something like: 0b00000000 00000000 00000000 00001010 and it's size would be 4 bytes because an int is 4 bytes. If curNumberentries is a char, then it would only write 1 byte ( 0b00001010 ) to the file.
  2. Yes
  3. You don't specify the location to read or write in the read/write functions. You must call .seek() to move the read/write pointer then call read()/write() to read/write to that location. It writes from the current position in the file to current position + sizeof( type ).
  4. Files are just a string of bits. If you start a new file it has a length of 0, now you write an 'A' to it. The ascii value of 'A' is 65, so the bit pattern 0b01000001 is written to the file. Next you write a '1' as an integer to the file, the file now holds 5 bytes with the bit patter:
    0b01000001 0b00000000 00000000 00000000 00000001 ( spaces are for clarity, they wouldn't be in the file nor the leading 0b ).
  5. If it seems like sorcery, it's because of lack of knowledge and experience. Learning how APIs can be a good way of demystifying things, but will take you down a long road. If you plan on completing projects, STAY FOCUSED. Learn how to use an API, not how it works. I got caught up in learning how the APIs were implemented instead of learning how to use them and continuing my own projects. I have yet to complete a project because of stuff like this.
On the one hand, I've gained a great deal of knowledge and experience and am able to answer questions like this, but on the other hand I have never been able to complete a project because I get caught up in the details.
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com

Post Reply