Simple File Splitter


/ Published in: C
Save to your folder(s)

Simple File Splitter. No JOINER YET!


Copy this code and paste it in your HTML
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <Windows.h>
  5.  
  6. int fileSize ( char File[] );
  7. void fileNameExt ( char Name[], char Ext[], char File[] );
  8.  
  9. int main( int argc, char *argv[] )
  10. {
  11. FILE *source, *destination;
  12. int size, parts;
  13. char name[100], ext[100], aux[100];
  14.  
  15. if( argc < 3 )
  16. {
  17. printf( "There aren't enough args!\n" );
  18. system( "pause" );
  19. return 0;
  20. }
  21.  
  22. fileNameExt( name, ext, argv[1] );
  23. parts = atoi( argv[2] );
  24.  
  25. size = fileSize( argv[1] );
  26. if( (source = fopen(argv[1], "rb")) == NULL )
  27. {
  28. printf( "Error! Source file could not be open!\n" );
  29. system( "pause" );
  30. return 0;
  31. }
  32.  
  33.  
  34. for( int i = 0; i < parts; i++ )
  35. {
  36. sprintf( aux, "%s%s-%d", name, ext, i );
  37. printf( "Generating %s file! -- ", aux );
  38.  
  39. if( (destination = fopen(aux, "wb")) == NULL )
  40. {
  41. printf( "Error! Destination file %s could not be created!\n", aux );
  42. system( "pause" );
  43. return 0;
  44. }
  45.  
  46. char car = getc( source );
  47. for( int j = 0; j < size/parts && !feof(source); j++ )
  48. {
  49. putc( car, destination );
  50. car = getc( source );
  51. }
  52.  
  53. printf( "OK!\n" );
  54. fclose( destination );
  55. }
  56.  
  57. fclose( source );
  58. system( "pause" );
  59. return 0;
  60. }
  61.  
  62. int fileSize( char File[] )
  63. {
  64. FILE *f;
  65.  
  66. if( (f = fopen(File,"rb")) == NULL )
  67. {
  68. printf( "Error! Source file could not be open!\n" );
  69. system( "pause" );
  70. return 0;
  71. }
  72.  
  73. fseek( f, 0,SEEK_END );
  74. int sz = ftell(f);
  75. fclose( f );
  76.  
  77. return sz;
  78. }
  79.  
  80. void fileNameExt( char Name[], char Ext[], char File[] )
  81. {
  82. char *aux;
  83. if( (aux = strrchr(File, '\\')) == NULL )
  84. {
  85. if( (aux = strrchr( File, '/' )) == NULL)
  86. {
  87. aux = File;
  88. }
  89. }
  90.  
  91. if( aux != File )
  92. {
  93. aux++;
  94. }
  95.  
  96. for( int i = 0; *aux != '.'; i++, aux++ )
  97. {
  98. Name[i] = *aux;
  99. Name[i+1] = '\0';
  100. }
  101.  
  102. for( int i = 0; *aux != '\0'; i++, aux++ )
  103. {
  104. Ext[i] = *aux;
  105. Ext[i+1] = '\0';
  106. }
  107. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.