function splittxtfile(infile,size,formattype,TEST)
% SPLITTXTFILE(infile,size,formattype,TEST)
%
% Splits text a text file into N lines, such that
% the file size is one line longer than BYTES.
% So if you specify 1 MB files, SPLITTXTFILE will
% split the infile into peices that are 1 MB + 1
% line
%
% inflile is the input file name.
% size is in bytes
% if formattype=0 [default]
% in.txt becomes 001in.txt, 002in.txt, ...
% if formattype=1
% in.txt becomes in.001, in.002, ....
% test=1 then the name of each file will be printed
%
% IT'S NOT FANCY BUT IT WORKS
% Michael Robbins
% robbins@bloomberg.net
% michael.robbins@us.cibc.com
% splittxtfile('TYtik-2003-04-25.csv',.5e9,0,1);
fid_in=fopen(infile,'r');
i=0;
d.bytes=Inf;
while 1
if d.bytes>size
i=i+1;
if formattype
outfile=[strtok(infile,'.') '.' sprintf('%03d',i)];
else
outfile=[sprintf('%03d',i) infile];
end;
if i>1 fclose(fid_out); end;
fid_out=fopen(outfile,'wt');
fprintf(TEST,'Opening %s for writing\n',outfile);
end;
tline = fgetl(fid_in);
if ~ischar(tline), break, end
fprintf(fid_out,'%s\n',tline);
d=dir(outfile);
end;
fclose(fid_out);
fclose(fid_in);