Hello, I wanted to calc a checksum from several files. In stead of getting the checksum of each file I wanted to attach all files into one big file, and then calc the checksum of that. Can I do this with ifstreams?
Thank you.
This is a discussion on Attach several ifstreams files into one? within the C++ Programming forums, part of the General Programming Boards category; Hello, I wanted to calc a checksum from several files. In stead of getting the checksum of each file I ...
Hello, I wanted to calc a checksum from several files. In stead of getting the checksum of each file I wanted to attach all files into one big file, and then calc the checksum of that. Can I do this with ifstreams?
Thank you.
You mean morph several files into one?
Yes, that's possible. Read one file at a time and write it to the new file:
Code:#include <fstream.h> const char* NewFileName = "NewFile.txt"; const char* OldFileName[] = {"PartOne.txt", "PartTwo.txt", "PartThree.txt"}; const int NrOfFiles = 3; int main() { //Data ofstream WriteFile; ifstream ReadFile; int Size; char* Buffer = NULL; //Open the file for writing WriteFile.open(NewFileName, ios::out | ios::binary); if(WriteFile.fail()) { return 0; } //Loop though all files for(int i=0; i<NrOfFiles; i++) { //Open the file ReadFile.open(OldFileName[i], ios::in | ios::nocreate | ios::binary); if(!ReadFile.fail()) { //Get the size of the file ReadFile.seekg(0, ios::end); Size = ReadFile.tellg(); ReadFile.seekg(0, ios::beg); //Read data into a buffer, then write it to a file Buffer = new char[Size]; if(Buffer != NULL) { ReadFile.read(Buffer, Size); WriteFile.write(Buffer, Size); delete[] Buffer; Buffer = NULL; } //Close the file ReadFile.close(); } } //Close the file and exit WriteFile.close(); return 0; }
MagosX.com
Give a man a fish and you feed him for a day.
Teach a man to fish and you feed him for a lifetime.
Thank you very much.