Thread: Attach several ifstreams files into one?

  1. #1
    robals
    Guest

    Attach several ifstreams files into one?

    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.

  2. #2
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    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.

  3. #3
    robals
    Guest
    Thank you very much.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Folding@Home Cboard team?
    By jverkoey in forum A Brief History of Cprogramming.com
    Replies: 398
    Last Post: 10-11-2005, 08:44 AM
  2. header and source files
    By gtriarhos in forum C Programming
    Replies: 3
    Last Post: 10-02-2005, 03:16 AM
  3. Batch file programming
    By year2038bug in forum Tech Board
    Replies: 10
    Last Post: 09-05-2005, 03:30 PM
  4. reinserting htm files into chm help files
    By verb in forum Windows Programming
    Replies: 0
    Last Post: 02-15-2002, 09:35 AM
  5. displaying text files, wierd thing :(
    By Gades in forum C Programming
    Replies: 2
    Last Post: 11-20-2001, 05:18 PM