Im trying a little mini project just because I got curious. I want to try to make a archive like file except nothing will be encrypted or compressed. My idea was the use the C++ filestream classes to read and write data from this archive file. To keep it simple I thought having a file structure that looked like this structure below would be a good start.
File structure:
sizeOfFile1
file1Name
file1Data
sizeOfFile2
file2Name
file2Data
.
.
.
But I have run into a little problem. My first approach is to get in idea how my functions would be like so I made a small program where I told the program to put a file into a archive file (only a single file would be archived) and then do the reverse. So in short just making a long way to make a copy of a file twice.
I was able to do this withI got a working copy. But the problem with this approach is that Im not able to read parts of a file.Code:outdata << indata.rdbuf();
This approach could work when archiving files by first writing a long representing the size of the file and then an fixed size array for the file name. Then after that use outdata << indata.rdbuf(); to write from the source file into my archive file and then repeat the process for each file.
But to unarchive this I would need to first read the size of a long, and then the size of my fixed array. And then use that long to read the right amount of data that will be the file itself and then output it. But I havenīt been able to do this by using either read, get or wrote. Im able to extract the long and the array. Atleast I have been able to in other mini projects. But I seem unable to extract the file even when itīs only the file that is stored inside the archive.
My approach to this it that I first get the file size of the file. (I have checked and I seem to get the right size)
Then I would make a char array and use it as a buffer to store the data while reading the file. But I think itīs here I have my problem.Code:// begin, end and size are of the type long begin = indata.tellg(); indata.seekg(0, ios::end); end = indata.tellg(); size = end - begin;
I use binary mode when I do this. When I use write to write the the output file from this buffer I get a file with the right size but that is corrupted. So Im sure I do something wrong here and itīs here I need your help to get my into the right direction.Code:// buffer is the char array and fileSize is the size of the file in bytes. With mingw on my // computer one char is 1 byte, so I made the char array with fileSize(in bytes) elements indata.read(reinterpret_cast<char*>(&buffer), fileSize);
How should I tackle this so I get the result that I want? My idea was the read a file in binary mode, save it inside another file, and now while "archiving" a single file I would be just making a copy with another extension. And then reverse the process. By renaming the output file back to the same extension as the original file I found that the file is corrupted. So I need some advice how to read the file in a proper way so I donīt miss any data and then not getting any corrupted files.
I do understand that it would be stupid to try large files because it would eat my memory so Im sticking to small files for now and will work on a way to read and write piece by piece later when I get this working. So far itīs only in testing and trying out stage.
This is just a project Im doing to learn a little more and just play around with. Not because I try to archive files for a real life use. At least not yet but who knows how things will be in the future. The one who lives will know.