[XenoCafe Logo] Click for Homepage
Home Tutorials Forum Blog Advertising Links Contact About

 



File Splitter for DOS - C Programming

Written by Tony Bhimani
September 7, 2005

Requirements
C compiler (I used Borland Turbo C++ 3.0)
MS DOS or Windows

Download the source code: fsplit.zip

The file splitter (FSPLIT) chops files larger than 1.44 MB into diskette sized chunks. FSPLIT has to be one of the most useless applications because of today's availability of USB flash drives, ZIP disks, CD and DVD burners, and all other high capacity media devices. However, this is today and when I wrote the program it was 1997. My motivation was to transfer DOOM from home to one of my school's lab computers so I could enjoy class time playing games instead of being bored out of my mind. FSPLIT is written in C and I offer it to all that might find it useful in some regard; be it the file reading and writing functions, changing the output file date and time back to original values before the split using inline Assembler, processing command line arguments, or any other aspect of its functionality. It was compiled with Borland Turbo C++ 3.0, but any C compiler should work. I don't believe I made the code dependent on anything specific to Borland. It won't work with long file names in its current state because of the structure I used limiting them to the DOS extent (FILENAME{8}.EXT{3}). There should be no problem altering the code to handle long names.

FSPLIT has two modes: split files and rebuild them. The modes are determined by the command line argument passed to it (-s or -b). Output is written to the same directory FSPLIT resides unless an output path is specified. The chunk files are named sequentially as FILE.1, FILE.2, FILE.3, and so on. A .LST file is created containing information about the file being split. It includes the original file name, attributes, date & time, size, and how many chunks of the file there are.

The .LST file is defined with this structure. Increase the filename array size to overcome the DOS file name limitation.

// LST file def
typedef struct
{
   char filename[13];        // filename
   int  attrib;              // file attributes
   int  file_time;           // file time
   int  file_date;           // file date
   long file_size;           // file size
   int  count;               // number of chunks the file is split into
} listfile_attribs;

Splitting Files

To split files, specify the input file and use the -s command. The last argument can be an optional output directly for where the chunks are written to.

Here is the command line syntax to use: fsplit -s c:\path\file.ext

Each output file can be put on a floppy and the last file, .LST, and program on the final one. Then everything can be transferred to another computer.

Rebuilding Files

To rebuild the file, specify the .LST file and the -b command.

Here is the command line syntax to use: fsplit -b c:\path\file.lst

I could have made the program prompt for each diskette instead of manually having to copy each file to the hard drive, but I was the only one going to use it.

Here are some of the helper functions in FSPLIT for your amusement.

/****************************************************************************
 Function Title : check_cmdline
 Description    : check the command line for program switches
 Return Value   : option_flag set to the selected options
 Example of Use : check_cmdline(argument_count,argument_strings);
****************************************************************************/
void check_cmdline(int argc,char *argv[])
{
  int i;  // loop variable

  // display a message, woohoo!!!
  printf("\nFSPLIT v1.0 by Tony Bhimani, Copyright (C) 1997\n"
         "Splits files that are too large to fit on a 1.44MB diskette\n"
         "into smaller files exactly the size of the diskette\n\n");

  // right amount of arguments?
  if (argc<3)
  {
    // display the command format
    printf("USAGE : FSPLIT <command> <filename> [output path]\n\n");
    display_help();   // display help
    exit(0);          // exit the program
  }

  // loop for the amount of arguments on the command line
  for (i=1; i<argc; i++)
  {
    if (argv[i][0] == '-')            // check for the '-' character
    {
      if (strcmp(argv[i],"-s") == 0)  // split the file switch?
        option_flag |= SPLIT_FLAG;    // yup, set the split flag bit
      else
      if (strcmp(argv[i],"-b") == 0)  // build the file switch?
        option_flag |= BUILD_FLAG;    // yup, set the build flag bit
    }
  }
}

/****************************************************************************
 Function Title : sftime
 Description    : set the date and time for a file
 Return Value   : none
 Example of Use : sftime(filehandle,filetime,filedate);
****************************************************************************/
void sftime(int handle,int time,int date)
{
   asm {
     mov ah,57h
     mov al,1
     mov bx,handle
     mov cx,time
     mov dx,date
     int 21h
   }
}

/****************************************************************************
 Function Title : filesize
 Description    : get the size of a file
 Return Value   : the filesize
 Example of Use : size = filesize(file);
****************************************************************************/
long filesize(FILE *stream)
{
  long curpos,length;                 

  curpos = ftell(stream);         // get the current position in the file
  fseek(stream,0L,SEEK_END);      // go to the end of the file
  length = ftell(stream);         // get the end position of the file
  fseek(stream,curpos,SEEK_SET);  // go back to where the file position was
  return length;                  // pass back the filesize
}

/****************************************************************************
 Function Title : UpperPath
 Description    : uppercases a string
 Return Value   : address to the uppercase string
 Example of Use : UpperPath("c:\"); -> "C:\"
****************************************************************************/
char *UpperPath(char *ThePath)
{
  int length,i;

  length = strlen(ThePath);             // length of the path
  for (i=0; i<length; i++)              // loop the length of the string
    ThePath[i] = toupper(ThePath[i]);   // uppercase the string

  return ThePath;                       // pass back the address of the string
}

/****************************************************************************
 Function Title : BuildFilePath
 Description    : builds a file path by attaching a filename
 Return Value   : address of the path
 Example of Use : BuildFilePath(PATHOUT,"c:\","filename.ext"); -> "c:\filename.ext"
****************************************************************************/
char *BuildFilePath(char *BuiltPath,char *path,char *filename)
{
  int len;

  strcpy(BuiltPath,path);               // copy the path
  len = strlen(BuiltPath);              // get it's length
  if (len >= 2 && BuiltPath[1] == ':')  
  {
    if (len != 2)
    {
      if (BuiltPath[len-1] != '\\')     // check for a backslash
      strcat(BuiltPath,"\\");           // append one on
    }
    strcat(BuiltPath,filename);         // attach the filename onto the path
    return BuiltPath;                   // pass the path address
  }
  else
  {
    printf("Invalid path specified\n"); // display an error
    fclose(in);                         // close the input file
    fclose(out);                        // close the output file
    fclose(lstout);                     // close the list output file
    free(buffer);                       // free the buffer
    exit(0);                            // exit the program
  }
  return BuiltPath;                     // pass back the path address
}

/****************************************************************************
 Function Title : create_buffer
 Description    : allocates memory for a buffer
 Return Value   : none
 Example of Use : create_buffer();
****************************************************************************/
void create_buffer(void)
{
  buffer = (char *)malloc(BUFFER_SIZE);     // allocate the buffer
  if (buffer == NULL)                       // couldn't allocate?
  {
    printf("Not enough memory to run\n");   // error message
    fclose(in);                             // close the input file
    fclose(lstout);                         // close the list file 
    exit(0);                                // exit the program
  }
}

Check the download file for the code on splitting and rebuilding the files. I wrote this stuff 8 years ago, so I doubt it has any educational value in 2005. Nevertheless, it's here and you can pick and choose what parts you want.



How would you rate the usefulness of this content?

Poor 1
2
3
4
5
6
7
8
9
Outstanding

Optional: Tell us why you rated the content this way.
Characters remaining: 1024
Average rating: 5.41 out of 9.

1 2 3 4 5 6 7 8 9
22 people have rated this content.
This page has been viewed 8,881 times
Copyright © 2004-2012 XenoCafe. All Rights Reserved. XenoCafe is Powered by Linux. Free your mind and your wallet. Switch to Linux.