I have written a simple subroutine to break a large file up into smaller increments. Since the large file contains records that are a certain blocksize I have abstracted the program to load the record block-size of that file and to calculate how many records can be written to each smaller file based on the upper limit file size. For some reason as I parse, there are several files that have written past the specified limit for my sub-files. I do not understand this behavior, especially when I am specifically reading a set number of bytes into my buffer. Please see the following code
my $increment = 1000000; #For this example $$config{blocksize} == 550 my $records = int($increment / $$config{blocksize}); my $bytecnt = $$config{blocksize} * $records; #After caluclations this prints out as 999900 print "bytecnt[$bytecnt]\n"; my $file = 0; open PARENT, $$self{data} or die "Cannot open [$$self{data}] for i +ncremental parsing\n"; while(1){ $file++; my $data = ""; if($file == 1){ read(PARENT, $data, $$config{startblock}); read(PARENT, $data, $bytecnt); open(FILE, ">tmp\\$$self{process}".sprintf("%02d", $file). +".dat") or die "Cannot open tmp\\$$self{process}".sprintf("%02d", $fi +le).".dat for incremental writing\n"; print FILE $data; close(FILE); next; } read(PARENT, $data, $bytecnt); open(FILE, ">tmp\\$$self{process}".sprintf("%02d", $file).".da +t") or die "Cannot open tmp\\$$self{process}".sprintf("%02d", $file). +".dat for incremental writing\n"; print FILE $data; close(FILE); if(eof(PARENT)){ last; } } close(PARENT); die;
After this routine runs I do a ls -l on my directory and the following is printed out.
-rwxr-x---+ 1 999946 May 30 09:27 patient01.dat -rwxr-x---+ 1 1000463 May 30 09:27 patient02.dat -rwxr-x---+ 1 999940 May 30 09:27 patient03.dat -rwxr-x---+ 1 999944 May 30 09:27 patient04.dat -rwxr-x---+ 1 999931 May 30 09:27 patient05.dat -rwxr-x---+ 1 999945 May 30 09:27 patient06.dat -rwxr-x---+ 1 1000236 May 30 09:27 patient38.dat -rwxr-x---+ 1 1000122 May 30 09:27 patient39.dat -rwxr-x---+ 1 1000031 May 30 09:27 patient41.dat
There are more files in the ls, but I am putting the files that operate outside the behavior that I expect. My confusion is, if my blocksize is constant, and my file length is constant, then how is it possible to be printing variable file lengths? The only file that should be different in length is the last one...

In reply to Unexpected File Results by Grundle

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.