split file with according to size

split file with according to size

am 02.01.2008 18:32:13 von Babu

Hi,

I have a input file called temp.hex (163840 bytes) . Is there any
function in perl that does the file splitting with size of the file
information

tempA.hex(81920 bytes) and tempB.hex(81920 bytes)
or

tempA.hex(3840 bytes) and tempB.hex(16000bytes)

Thanks in advance

Sn

Re: split file with according to size

am 02.01.2008 18:57:09 von it_says_BALLS_on_your forehead

On Jan 2, 12:32=A0pm, Babu wrote:
> Hi,
>
> =A0 I have a input file called temp.hex (163840 bytes) . Is there any
> function in perl that does the file splitting with size of the file
> information
>
> tempA.hex(81920 bytes) and tempB.hex(81920 bytes)
> or
>
> tempA.hex(3840 bytes) and tempB.hex(16000bytes)
>

There's a Unix utility which does this. The name of the utility is,
oddly enough, split.

split -b 81920

Re: split file with according to size

am 02.01.2008 19:03:19 von Purl Gurl

Babu wrote:

> I have a input file called temp.hex (163840 bytes) . Is there any
> function in perl that does the file splitting with size of the file
> information


Use of "read" is a very direct approach.

Other keyterms for research are, sysread, seek, tell, truncate and eof (end of file).

From Perl documentation:

***

read FILEHANDLE,SCALAR,LENGTH,OFFSET
read FILEHANDLE,SCALAR,LENGTH

Attempts to read LENGTH characters of data into variable SCALAR
from the specified FILEHANDLE. Returns the number of characters
actually read, 0 at end of file, or undef if there was an error.
SCALAR will be grown or shrunk to the length actually read. If
SCALAR needs growing, the new bytes will be zero bytes. An OFFSET
may be specified to place the read data into some other place in
SCALAR than the beginning. The call is actually implemented in terms
of either Perl's or system's fread() call. To get a true read(2)
system call, see sysread.

Note the characters: depending on the status of the filehandle,
either (8-bit) bytes or characters are read. By default all
filehandles operate on bytes, but for example if the filehandle
has been opened with the :utf8 I/O layer (see open, and the open
pragma, the open manpage), the I/O will operate on characters,
not bytes.

***

Use of "offset" will prove helpful for your task.

You can split apart a hex file but there is no guarantee you
can modify then paste back together a hex file, and have this
file operate correctly, if part of software.

Printing hex data to a file "might" prove problematic. Many
hex characters are outside of a character set "typically"
expected by perl core. This may or may not be a problem
depending on how you handle your data and depending on your
operating system.


This google link will lead you to a lot of reference resources,

http://www.google.com/search?hl=en&q=split+file+hex+perl&btn G=Google+Search


--
Purl Gurl
--
So many are stumped by what slips right off the top of my mind
like a man's bad fitting hairpiece.

Re: split file with according to size

am 02.01.2008 20:56:29 von jurgenex

Babu wrote:
> I have a input file called temp.hex (163840 bytes) . Is there any
>function in perl that does the file splitting with size of the file
>information
>
>tempA.hex(81920 bytes) and tempB.hex(81920 bytes)
>or
>
>tempA.hex(3840 bytes) and tempB.hex(16000bytes)

No, Perl does not have a function with this functionality. It is a rather
special request and it would be odd, if Perl as a general purpose language
would provide a separate function for each and every uncommon file handling
request.
But is trivial to write your own. Just open() the source and target files,
read() as many bytes as you want to go into the first target file, print()
those bytes to the first target file, and then repeat read()ing and
print()ing for the second target file.

However I would just use the external split program instead. Why reinvent
the wheel after all?

jue