View Full Version : n00b in need of C++ help.
sb iq
November 12th, 2006, 09:38
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.
euphoria
November 12th, 2006, 10:13
Get linux, it has a free C/C++ compiler called GCC ;) And it's good.
sb iq
November 12th, 2006, 10:34
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
November 12th, 2006, 11:14
Check out Dev-C++ (http://bloodshed.net/). 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:
#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)
smcd
November 12th, 2006, 22:44
For a "Visual C++ 6" type feel of environment, you might try MinGW Studio, Click here to go to http://www.parinyasoft.com/ (http://www.parinyasoft.com/) it is free and there are download options to have it bundled with a compiler package as well.
sb iq
November 13th, 2006, 00:04
Toasty, thank you SO MUCH for posting that! ^_^
Toasty
November 13th, 2006, 00:48
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
pegasus001
November 14th, 2006, 17:59
One c++ compiler for windows is devc++, just google it and you will surely find it. It`s not more than 10 mb.
Garstyciuks
November 14th, 2006, 19:15
Didn't Toasty already mentioned it and included a link to it?
pegasus001
November 16th, 2006, 16:58
Didn't Toasty already mentioned it and included a link to it?
Sorry , i just read it. It was dummy of me but i replied in a rush last time. Won`t happen again.
Powered by vBulletin® Version 4.1.10 Copyright © 2012 vBulletin Solutions, Inc. All rights reserved.