How to split a file in bytes implemented in C language?
I have a file whose size is 300 bytes. I want to split it into several bytes (example: (150 bytes each) or (3 100 bytes)). How do I implement this in C Language?
create two FILES, say *Infile and *Outfile, a char Namebuffer[] (make sure you have enough memory for this) and a size_t variable called counter (I believe that’s a long but it varies from compiler to compiler. Read stdio.h for a definition of size_t). Also declare an int Filecounter. Input file name and size of chunks to split. Open Infile. Set Filecounter to 0 and sprintf(Namebuffer, "%s.%3d", argv[1]{Your infile},Filecounter);Open Outfile with the name in Namebuffer while not EOF(Infile) read a char from infile, write a char to outfile, and increment counter. If counter mod the size of your chunks ==0, close Outfile, increment Filecounter sprintf a new filename into namebuffer (with the new Filecounter) open outfile as namespace, and go back to work. When you reach end of file just close everything.
I would check out the read() function. You can specify how many bytes you want to read and then you can write those corresponding bytes to a file. Or, if the files are going to be small enought (like your examples of 300 B), you could just read the whole file into memory and split it up there. Make sure you don’t do this with large files though or you will run out of physical memory and the computer will slow wayyyy down