Thread: Cutting up files

  1. #1
    Confused
    Join Date
    Nov 2002
    Location
    Warwick, UK
    Posts
    209

    Cutting up files

    Sometimes, you may want to cut up files to make them transportable by floppy disk. I'm trying to write a program to do this, but it's not working, as usual :P Here's what I've got :

    Code:
    void cut(char* filename, int filesizes)
    {
        clrscr();
        fin.open(filename, ios::in | ios::nocreate);
        if(!fin)
        {
            t("File not found. Exiting program...");
            Sleep(1500);
            exit(EXIT_SUCCESS);
        }
        string fileout = "";
        int counter = 0;
        int counter2 = 0;
        char temp[1];
        
        while(!fin.eof())
        {
            counter++;
            counter2 = 0;
            fin >> temp[0];
            fileout += filename;
            fileout += ".";
            fileout += counter;
            fileout += ".rps";
            fout.open(fileout.c_str(), ios::out);
            while(counter2 < 1024*filesizes)
            {
                    if(fin.eof())
                    {
                            fin.close();
                            fout.close();
                            exit(EXIT_SUCCESS);
                    }
                    cout << "1, "; 
                    fout << temp[0];
                    counter2++;
            }
            fout.close();
            cout << "ONE FILE WRITTEN ! --------------------" << endl;
            Sleep(1000);
        }
        
    }
    As you can see, I annotated it to see what happens, but I get no files written, and I get this continuing past the EOF...

    If I explain my code : the user types in a filename, and a filesize for each of the smaller files to be. This size is in kilobytes, which is why I write out 1024 times this number, as I write out a byte at a time. Void t is simply a typewriter effect for cout. It's probably something I've missed, but I can't see it.

    Thanks for your help.

  2. #2
    Registered User
    Join Date
    Jul 2003
    Posts
    7
    It seems like what you're looking for is what split does. Split can be found here:

    http://www.gnu.org/directory/textutils.html

    The source is available to you from there as well if you just want to see how they did what they did.

    ~SK

  3. #3
    Confused
    Join Date
    Nov 2002
    Location
    Warwick, UK
    Posts
    209
    Thanks, I'll have a look at that. However, can you tell me what's wrong with my code ? Thanks.

  4. #4
    Registered User
    Join Date
    Jun 2003
    Posts
    245
    Your code is a fragment and doesn't contain any comments whatsoever, so it's difficult to see what you are trying to do.

    However, one thing I can see is that your "while(counter2 < 1024*filesizes)" loop never reads from the input file, which I think it's not what you want...

  5. #5
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    first of all, open the file with std::ios::binary, second get the file size from a call to seekg(0,std::ios::end) and tellg() third, use the file size that you got instead of the eof() call to tell you got to the end. That is all. Goodbye.
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. add source files to embedded VC 4.0
    By George2 in forum C++ Programming
    Replies: 4
    Last Post: 06-13-2006, 03:28 AM
  2. *.cpp and *.h files understanding
    By ElastoManiac in forum C++ Programming
    Replies: 4
    Last Post: 06-11-2006, 04:45 AM
  3. Linking header files, Source files and main program(Accel. C++)
    By Daniel Primed in forum C++ Programming
    Replies: 3
    Last Post: 01-17-2006, 11:46 AM
  4. Multiple Cpp Files
    By w4ck0z in forum C++ Programming
    Replies: 5
    Last Post: 11-14-2005, 02:41 PM
  5. header and source files
    By gtriarhos in forum C Programming
    Replies: 3
    Last Post: 10-02-2005, 03:16 AM