Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Fixed length flat file manipulations

Status
Not open for further replies.

pnad

Technical User
May 2, 2006
133
US
Hi,

I have a fixed length flat file - each 'record' in the file has a length of 80. I need to read this file and then extract data from various positions within each record and then write it to another file.

For example: the input file has data like this:

74807932020002100010020808 0000002355 26240992 74807932020002100011020808 0000090000 262409
74807932020002100011020808 0000090000 26240993 74807932020002100012020808 0000078654 262409
74807932020002100012020808 0000078654 26240994 74807932020002100013020808 0000055023 262409

I need to extract account number which is in pos 3 - 12 and then I need amount which is in pos 30 - 39 and so on...How do I do that?

Please help !

I have started writing code to read the file:

$filename = "input.txt" ;
$dataFile = fopen( $filename, "r" ) ;

if ( $dataFile )
{
while (!feof($dataFile))
{
$buffer = fgets($dataFile, 4096);
echo $buffer;
}

fclose($dataFile);
}
else
{
die( "fopen failed for $filename" ) ;
}

but after this, I am kinda stuck on how I can read each 'record' at a time and extract data from each 'record'?

Thanks.
 
Code:
function getData($file){
 $lines = file($file);
 $items = array();
 foreach($lines as $line){
   $accountNumber = substr($line, 3, 9);
   $amount = substr($line, 30, 9);
   $items[] = array($accountNumber, $amount);
 }
 return $items;
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top