I'm proud and happy you wrote that.
I didn't even have to explain how to do two files at once! =] Good job on that. Really.
Now for figuring out the file size, I mentioned fseek/ftell/seekg/tellg before correct?
fseek/ftell is C code. C++ uses iostreams which append a g to the end and gets rid of the f. the G generally stants for "Get". The F was removed because it's no longer File Get. In Cpp it's stream get.
Anyway enough about that since you aren't using ios file reading, I will demonstrate ftell/fseek as your code is C-Style file IO.
C++ Code:
void GetFileSize
(const char* FileNamePath
){ FILE
* pFile
= fopen(FileNamePath
, "rb"); //rb means read in binary mode. You DO NOT need binary mode but it depends on the kind of file. I tend to leave it there. if (pFile
!= NULL
) { fseek(pFile
, 0, SEEK_END
); //First we set the pointer to the very end of the file. This is good because when we ask to tell, it will just check where the pointer is. long size
= ftell(pFile
); //Pointer is at the end of the File. We ask it to tell use the size of the file in bytes! fclose(pFile
); //Always remember to close the file. It is a pointer/handle! printf("Our File Size In BYTES is: %ld \n", size
); //Print our file size in bytes! This is not bits, not megabytes, not megabits. It's bytes. } //When creating a buffer for the reading, you can do: char* Buffer
= new buffer
[size
]; //provided that size is declared in this scope. Currently it is not. It's in the scope of the if statement above. //Read the file into the buffer.. Do whatever you want with the data! //BUT ALWAYS, and I mean always in capitals: Remember to delete[] that buffer after! delete
[] Buffer
;//You most likely also want to reset the file pointer using rewind(pFile); After getting it's size.}
For the guy above, EOF means end of file. That's not getting file size. In Cpp it's istream::seekg and isstream::tellg. ofstream::seekp as well for pointers. While(!EOF) can also give undefined behaviour and cause an infinite loop. It's better to just get file size and read in one fell swoop unless you're tokenizing the data. Neat pseudo though.