File splitter

This is a discussion on File splitter within the C Programming forums, part of the General Programming Boards category; Hi! I want to create a program "file splitter". The whole file size is 5 MB. My problem is that ...

  1. #1
    Code Warrior
    Join Date
    Nov 2001
    Posts
    669

    Question File splitter

    Hi!

    I want to create a program "file splitter". The whole file size is 5 MB. My problem is that I do not know how to create files .001, .002, and so on, and every file has maximum size 1.4 MB.

    Any ideas?
    Current projects:
    1) User Interface Development Kit (C++)
    2) HTML SDK (C++)
    3) Classes (C++)
    4) INI Editor (Delphi)

  2. #2
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,386
    The filename is known, so you only need to adapt the extension.

    Open the inputfile as binary and create the new file, also binary and copy the bytes to the new file until 1.4 MB is reached. Then you close the new file and open a new file. This new file will also be filled with bytes. Do this until the end of the inputfile is reached.

    You can use the function feof to check if the end of the input file is reached:

    while (!feof (inputfile))
    {
    ..
    }

    The bytes can be read using the function fgetc:

    inputbyte = (unsigned char) fgetc (inputfile);

    And bytes can be written using fputc:

    fputc ((int) outputbyte, outputfile);

    Hope these hints will help. Note that there are other ways to implement the algorithm, this is just one of them.

  3. #3
    Code Warrior
    Join Date
    Nov 2001
    Posts
    669
    How do I adapt the extension? This part of the program is bugging me. Is there any command to do that?
    Current projects:
    1) User Interface Development Kit (C++)
    2) HTML SDK (C++)
    3) Classes (C++)
    4) INI Editor (Delphi)

  4. #4
    Registered User
    Join Date
    Dec 2001
    Posts
    34
    The following code will format a file name with an extension you can control.

    Code:
    int main(void)
    {
    	char FileName[31];
    	int i;
    
    	for (i = 0; i < 5 ; i++)
    	{
    	     sprintf(FileName, "filename.%03d", i);
    	     printf("%s\n", FileName);
    	}
    return 0;
    }
    Output:
    filename.000
    filename.001
    filename.002
    filename.003
    filename.004

  5. #5
    Code Warrior
    Join Date
    Nov 2001
    Posts
    669
    Thanks.
    Current projects:
    1) User Interface Development Kit (C++)
    2) HTML SDK (C++)
    3) Classes (C++)
    4) INI Editor (Delphi)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Data Structure Eror
    By prominababy in forum C Programming
    Replies: 3
    Last Post: 01-06-2009, 09:35 AM
  2. Encryption program
    By zeiffelz in forum C Programming
    Replies: 1
    Last Post: 06-15-2005, 04:39 AM
  3. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 11:12 PM
  4. Hmm....help me take a look at this: File Encryptor
    By heljy in forum C Programming
    Replies: 3
    Last Post: 03-23-2002, 10:57 AM
  5. Need a suggestion on a school project..
    By Screwz Luse in forum C Programming
    Replies: 5
    Last Post: 11-27-2001, 02:58 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21