Page 1 of 1

need to create a fixed placement flat-file

Posted: Tue Dec 20, 2005 3:50 pm
by Burrito
I need to build a system that can generate flat-text files with items in fixed positions.

for example:

First Name: position 1 - 99
Last Name: position 100 - 199
Phone Num: position 200-220
etc.

I'd like some suggestions as to what you think would be the best way to tackle this. My first concern is that the length of names, phone numbers etc is going to be variable and I need to account for those differences. I'm not sure of a php function that will allow me to continue a string at a pre-defined placement but if there is such a thing...that would be my golden key. My alternative (as I see it) is to figure out the length of the item, then add spaces until I hit the next spot which is not the option I want to pursue as there are a TON of 'fields' that I need to account for.

If any of you have done something like this, or know of a good way to tackle this project, please share as I really don't want to have to 'hard-code' this for all of the fields that are in this monstrosity.

Posted: Tue Dec 20, 2005 4:55 pm
by onion2k

Code: Select all

printf("[%-100s][%-200s][%-50s][%-100s]",$strVariable1,$strVariable2,$strVariable3,$strVariable4);
Something like that.. you might need to read the printf() manual page for specific formatting options.

Edit: If you wanted to print on a line from a specific location..

Code: Select all

function printat($line, $position, $string) {
  if (strlen($line)<$position) {
    $line.=str_repeat(" ",$position-$line);
  }
  $line.=$string;
}
Something along those lines, but with more case handling for when the string is too long, natch.

Posted: Tue Dec 20, 2005 4:57 pm
by josh
dbase is fixed length, but just make the field lengths larger then you need them, or when you do need to go over that length, create a new flatfile with the new field length, delete the old one, copy the new one back to where the old one was

http://us3.php.net/dbase


For variable length flatfiles, I would either recommend an xml file, or for a "real" flatfile approach, just have a different file for each field, with each record having a corresponding line in each file

You also might want to check out isam flatfiles, I think they even support indexing

Posted: Tue Dec 20, 2005 5:06 pm
by Burrito
onion2k -

I'll give that a whirl tomorrow as I think it looks like its headed in the direction I need to go.

a sample might be (and this would be data I'm selecting off of a MySQL db).

Code: Select all

Bob          Jones                888-888-8888                  444-44-4444  1975-12-22                                       Married
Larry        Smith                777-777-7777                  333-33-3333  1964-11-02                                       Single
as you can see, the variable sizes are not the same but the placement of the next 'item' must be the same. I assume your solution would work and as I said I will tackle it tomorrow and let you know.

thx for the input,

Burr

Posted: Tue Dec 20, 2005 5:17 pm
by josh
Burr-

why reinvent the wheel? dbase does exactly what you're doing, except it takes care of all the stuff for you, it's just as easy to use as the mysql functions. No more needing to worry about what to do when you remove a few rows from your flatfile, just call dbase_pack and you're done