splitting a file

A

Afifov

How can I split a file into several files each with a fixed size?
I recall that on linux there is a command that has a counter for file size
in bytes but cant seem to remember.

Help is appreciated.
 
M

Michael Mair

Afifov said:
How can I split a file into several files each with a fixed size?
I recall that on linux there is a command that has a counter for file size
in bytes but cant seem to remember.

Help is appreciated.

As this is comp.lang.c and not some Linux newsgroup, I suggest you
get the name of the file to be split, the maximum size S of the parts,
fopen() the file in binary mode and then
1) Try to fread() up to S bytes with return of R read bytes
2) Try to open the file for the next part (do you want to overwrite
or die on error?)
3) fwrite() R bytes to the current output file, close it.
4) if R<S, terminate. Else continue at 1.

fclose() your source file.
Done.

<OT>Have you tried 'man split'?</OT

Cheers
Michael
 
R

Richard Heathfield

Afifov said:
How can I split a file into several files each with a fixed size?

All the same size? If the file size is composite, this is quite easy. If,
however, the file size is prime, the task is rather more difficult, as it
will involve splitting the byte into subytomic particles. Doable, but
requires a lot of energy.

If you are prepared for the various output files to be of various sizes,
this becomes trivial. You will need:

fopen, fread (or getc), fwrite (or putc), fclose

Curiously, if you know the largest file size you are willing to /write/,
then you almost certainly don't need to know the file size in advance.

Try writing the program. You'll probably be amazed at how simple it is. But
if you get stuck, show us your best shot.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top