Convert Minecraft server world files into MCserver world files.
#37
Well good job getting it to work.

I see you're not exactly using the fread() function to its full potentialTongue
Code:
char temparr[compdlength]; //can't get fread to read more than one char at a time into a char array... so that's what I'll do.  :(    At least it works.
int re = 0;
char tempbyte = 0;
while (re < compdlength) { //loop through file and read contents into char array a byte at a time.
    if( fread( &tempbyte, sizeof(tempbyte), 1, f) != 1 ) { cout << "ERROR rf22 READING FROM FILE " << SourceFile; fclose(f); return false; }
    temparr[re] = tempbyte;
    re++;
    frloc++;
}

Allow me to explain how you might want to use it instead.

You created the array temparr with exactly the amount of bytes you want to read and store in it. In C++ this translates to a stream of bytes that are all placed after each other in memory, so if you know the beginning of that stream (memory address) you can fill all the bytes in one go with a single fread() call.
The first argument of fread() takes a memory address (the & sign gets the memory address of a variable) to read the data into, the second argument takes the amount of bytes you want to read (this should never exceed the amount of bytes you have reserved for storage: temparr), the third argument takes how many times you want to read this data (I don't really understand why, I just always use 1) and the last argument takes the file handle.

Now if you know this, you can change the above code into something compacter and much faster
Code:
char temparr[compdlength];
if( fread( chartemparr, compdlength, 1, f) != 1 ) { /*error out*/ }

You see I don't use the & sign, this is because the variable for an array IS a memory address, and any number between square brackets ( [ ] ) simply give an offset.

I hope this gives you a better understanding of how C++ worksTongue
Reply
Thanks given by:


Messages In This Thread
RE: Convert Minecraft server world files into MCserver world files. - by FakeTruth - 10-30-2011, 10:17 AM



Users browsing this thread: 1 Guest(s)