Results 1 to 10 of 10
  1. #1
    EmuTalk Member
    Join Date
    Aug 2005
    Posts
    74

    n00b in need of C++ help.

    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.


    • Advertising

      advertising
      EmuTalk.net
      has no influence
      on the ads that
      are displayed
        
       

  2. #2
    Emutalk Member euphoria's Avatar
    Join Date
    Jan 2002
    Location
    60° 10' N 24° 57' E
    Posts
    308
    Get linux, it has a free C/C++ compiler called GCC And it's good.

  3. #3
    EmuTalk Member
    Join Date
    Aug 2005
    Posts
    74
    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.

  4. #4
    Sony battery Toasty's Avatar
    Join Date
    Dec 2004
    Location
    Oregon, U.S.A.
    Posts
    2,048
    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. )
    Last edited by Toasty; November 12th, 2006 at 10:30.

  5. #5
    Moderator smcd's Avatar
    Join Date
    Jun 2004
    Posts
    2,503
    For a "Visual C++ 6" type feel of environment, you might try MinGW Studio, Click here to go to http://www.parinyasoft.com/ it is free and there are download options to have it bundled with a compiler package as well.

  6. #6
    EmuTalk Member
    Join Date
    Aug 2005
    Posts
    74
    Toasty, thank you SO MUCH for posting that! ^_^

  7. #7
    Sony battery Toasty's Avatar
    Join Date
    Dec 2004
    Location
    Oregon, U.S.A.
    Posts
    2,048
    My pleasure. Sorry the code is void of comments, but I was tired and in a hurry, so you get what you pay for.
    Last edited by Toasty; November 12th, 2006 at 23:51.

  8. #8
    Normal User pegasus001's Avatar
    Join Date
    Sep 2006
    Location
    Tirana
    Posts
    67
    One c++ compiler for windows is devc++, just google it and you will surely find it. It`s not more than 10 mb.

    ________________________________________
    Remember your youth with Naruto.

  9. #9

  10. #10
    Normal User pegasus001's Avatar
    Join Date
    Sep 2006
    Location
    Tirana
    Posts
    67
    Quote Originally Posted by Garstyciuks View Post
    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.

    ________________________________________
    Remember your youth with Naruto.

Similar Threads

  1. Replies: 9
    Last Post: May 9th, 2006, 21:03
  2. zaba is n00b Part 3
    By Knuckles in forum IRC Quotes
    Replies: 16
    Last Post: August 31st, 2004, 05:18
  3. Happy birthday n00b! (and not only)
    By PsyMan in forum Talk of the Town
    Replies: 12
    Last Post: August 27th, 2004, 00:03
  4. The moron n00b gets pwned
    By vampireuk in forum IRC Quotes
    Replies: 18
    Last Post: October 1st, 2003, 09:40
  5. n00b, with a n00b question....
    By BlingBGBling in forum TechTalk
    Replies: 35
    Last Post: March 8th, 2002, 04:28

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •