For Programmers: Free Programming Magazines  


Home > Archive > PHP Programming > August 2006 > last line of data written twice









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author last line of data written twice
Patrick

2006-08-29, 6:58 pm

Hi All,

Can someone take a look at my script and tell me if I am missing
something as to why the last line of data from my input file is being
written twice in the echo. I have included a short sample of the data
and the script.

Thanks,
Patrick

***Please note that data in this particualr file begins after first
line. I don't have control over this, it's the way I receive the data.***



Unit: T4
Mission: 383b
Cycle: 80
Stage: Ascent
Cycle Start Time: 08-22-06 04:20:11
Stage Time: 08-22-06 04:20:11
Latitude: 90.0000 Longitude: -360.0000
CTD 61.152 30.38 26.94 36.54
CTD 61.165 30.38 25.92 36.55
CTD 61.151 30.38 24.71 36.55
CTD 61.138 30.38 23.65 36.54
CTD 61.125 30.37 22.06 36.54
CTD 61.121 30.37 20.99 36.54
CTD 61.099 30.37 19.87 36.53

<?php
echo "<pre>\n";

// Assigns path to file a variable name
$atttxt = $filepath = "/usr3/BSOP/TEST/cycle-44ea88fb.att.txt";

// Opens file into variable
$readfile = fopen($atttxt, "r");

$i=0;

//***NOTE*** Line one in cycle files is empty. Data starts at
position 1

while(!feof($readfile))
{
// Reads file
$data = fgets($readfile);

if ($i == 1)
{
sscanf($data, "%s %s", $slop, $unit);
}

if ($i == 2)
{
sscanf($data, "%s %s", $slop, $mission);
}

if ($i == 3)
{
sscanf($data, "%s %s", $slop, $cycle);
}

if ($i == 4)
{
sscanf($data, "%s %s", $slop, $stage);
}
if ($i == 5)
{
sscanf($data, "%s %s %s %s %s", $slop, $slop, $slop,
$date, $time);
}

// Read of line 6 skipped due to duplicat value on line five
if ($i == 7)
{
sscanf($data, "%s %s %s %s", $slop, $lat, $slop, $lon);
}

$hline = "$unit $mission $cycle $stage $lat $lon $date $time";

If ($i >= 8)
{
sscanf($data, "%s %s %s %s %s", $slop, $cond, $temp, $press, $sal);

//echoing $i only for visual test of output

echo "$i $hline $cond $temp $press $sal\n";
}

$i = $i +1;

}

// Closes file
fclose ($readfile);

echo "</pre>\n";
?>

--
Patrick A. Smith Assistant System Administrator
Ocean Circulation Group – USF - College of Marine Science
http://ocgweb.marine.usf.edu Phone: 727 553-3334

The trouble with doing something right the first time is that nobody
appreciates how difficult it was. - La Rochefoucauld


Alvaro G. Vicario

2006-08-29, 6:58 pm

*** Patrick escribió/wrote (Tue, 29 Aug 2006 16:06:00 -0400):
> Can someone take a look at my script and tell me if I am missing
> something as to why the last line of data from my input file is being
> written twice in the echo. I have included a short sample of the data
> and the script.


I'd say it's because of the trailing line endings in the data file. If you
add more line endings, you get more duplicate lines.

You never reset the variables you use in sscanf() so for every line where
expected input is not found, the keep their previous value.

--
-+ http://alvaro.es - Álvaro G. Vicario - Burgos, Spain
++ Mi sitio sobre programación web: http://bits.demogracia.com
+- Mi web de humor con rayos UVA: http://www.demogracia.com
--
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2010 codecomments.com