What's new

n00b in need of C++ help.

sb iq

New member
Here is my problem:

I have a file with raw data that is 18304 bytes. It is raw, no file extension. I need to split my file into smaller 768-byte files. This means I should end up with 71 files that are 768 bytes, and one file that is 384 bytes.

Now, obviously, I sure as hell don’t want to open up my file in Notepad or Hex Workshop, and cut and paste data into new files 72 times. I’d get carpel-tunnel syndrome and probably go insane.

I need to write a C++ program that will do this for me.

The problem is it’s been years since I’ve taken a C++ course and I forgot a lot, and I mean a lot.

How should my code look like? Can someone explain this to me?

Also, where can I download a good free C++ compiler program? I lost my Microsoft Visual C++ installation CDs years ago. :(

Any help would be greatly appreciated, as I sure as Hell don’t want to cut, paste, and save files over and over again 72 times.
 
OP
S

sb iq

New member
Are there any good free C++ compilers for XP? Sorry to sound like a n00b, but I have used Linux/Unix environments in one of my classes, and I wasn't too comfortable around it.
 

Toasty

Sony battery
Check out Dev-C++. It's fairly simple to set up and use. Here's a fairly crude program I whipped up that should do what you want (there are already many available to accomplish this function, but you may benefit from seeing the source code) - just copy and paste it into Dev-C++ if you like:
Code:
#include <iostream>
#include <fstream>
#include <sstream>
#include <math.h>

using namespace std;

const unsigned int BUFFER_SIZE = 1024;

int main(int argc, char *argv[]) {
    
    char name[256];
    unsigned int chunkSize;
    
    start:
    cout << "Enter path to file to be split: ";
    cin >> name;
    
    cout << "Enter size of each chunk in bytes: ";
    cin >> chunkSize;
    
    if(chunkSize == 0) {
        
        cout << "Zero is bad. :(  Aborting..." << endl;
        goto start;
        
    }
    
    ifstream fileIn(name, ios::in | ios::binary);
    
    if(!fileIn) {
        
        cout << "Error opening file.  Aborting..." << endl;
        goto start;
        
    }
    
    fileIn.seekg(0, ios::end);
    streampos fileLength = fileIn.tellg();
    fileIn.seekg(0, ios::beg);
    
    char copyBuffer[BUFFER_SIZE];
    
    for(unsigned int chunkNumber = 0; ; ++chunkNumber) {
        
        if(fileLength <= 0) break;
        
        cout << "Writing chunk number " << chunkNumber << "..." << endl;
        
        ostringstream chunkNameStream;
        chunkNameStream << name << chunkNumber;
        
        string chunkName = chunkNameStream.str();
        
        ofstream chunkOut(chunkName.c_str(), ios::out | ios::binary | ios::trunc);
        
        if(!chunkOut) {
            
            cout << "Error creating \"" << chunkName << "\".  Aborting..." << endl;
            goto start;
            
        }
        
        unsigned int chunkCopied = 0;
        
        while(fileLength > 0 && chunkCopied < chunkSize) {
            
            unsigned int toBeCopied = min((unsigned long long) min(BUFFER_SIZE, chunkSize - chunkCopied), (unsigned long long) fileLength);
            unsigned int copied = fileIn.readsome(copyBuffer, toBeCopied);
            chunkOut.write(copyBuffer, copied);
            
            fileLength -= copied;
            chunkCopied += copied;
            
        }
        
        chunkOut.close();
        
    }
    
    fileIn.close();
    
    cout << "Operation complete." << endl;
    
    goto start;
    
    return 0;
    
}
EDIT: Just as a side note, it will only work for file chunk sizes up to 4GB when compiled for 32-bit systems. (This could easily be fixed with little modification, but I'm off to bed right now. :p)
 
Last edited:

Toasty

Sony battery
My pleasure. Sorry the code is void of comments, but I was tired and in a hurry, so you get what you pay for. :p
 
Last edited:

pegasus001

Normal User
One c++ compiler for windows is devc++, just google it and you will surely find it. It`s not more than 10 mb.
 

Top