Here is a small program I wrote because I needed to use
it.

It takes a file, and splits it into pieces, whose size
is specified on the command line.

I named it hack because:
1. It is. (A hack!).
2. It hacks the file into pieces brutally.
3. All the good names (cut, split) were taken.

Share, read and enjoy. I'm putting it in the public domain.

-----------------------------------------------------------
Moshe Zadka - moshez (at) math (dot) huji (dot) ac (dot) il
What have YOU done today to promote Linux? I'm using 2.1.57
My moto: Signatures should not be longer then 4 lines.
#include <stdio.h>

int main(int argc, char **argv)
{
        char name[1024];
        int size;
        int count;
        int c;
        FILE *fin;
        FILE *fout;

        if(argc<3) {
                fprintf(stderr, "Error usage\n");
                exit(1);
        }

        size=atoi(argv[1]);
        fprintf(stderr, "size is %d\n", size);
        
        if((fin=fopen(argv[2], "r"))==NULL) {
                fprintf(stderr, "Error file\n");
                exit(1);
        }
        fprintf(stderr, "opened %s\n", argv[2]);

        if(feof(fin))
                fprintf(stderr, "Empty file\n");

        for(count=0;!feof(fin);count++) {
                int i;
                sprintf(name, "%s.%03d", argv[2], count);
                fprintf(stderr, "opening %s\n", name);
                if((fout=fopen(name, "w"))==NULL) {
                        fprintf(stderr, "Error out file\n");
                        exit(1);
                }
                fprintf(stderr, "opened %s\n", name);
                for(i=0;i<size;i++) {
                        if((c=fgetc(fin))!=EOF)
                                fputc(c, fout);
                        else
                                break;
                }
                fclose(fout);
                fprintf(stderr, "closed %s\n", name);
        }
}

Reply via email to