This is Interesting: Free Magazines for Graphics designers and webmasters
Home > Archive > Webmaster forum > August 2006 > last line of data being 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 being written twice
|
|
| Patrick 2006-08-30, 6:43 pm |
| Hi All,
Can someone (Grey?) take a look at my code 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 asked this over in a PHP group and only got one reply. He suggested
that it was because the data file had an additional line at the end. I
had already checked this.
Thanks,
Patrick
***Please note that data in this particular file begins after the first
line, position zero. I don't have control over this, it's the way I
receive the data.***
The input file sample.
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
The code
<?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", $s1, $unit);
}
if ($i == 2)
{
sscanf($data, "%s %s", $s2, $mission);
}
if ($i == 3)
{
sscanf($data, "%s %s", $s3, $cycle);
}
if ($i == 4)
{
sscanf($data, "%s %s", $s3, $stage);
}
if ($i == 5)
{
sscanf($data, "%s %s %s %s %s", $s4, $s5, $s6, $date,
$time);
}
//Skipping line 6, duplicate data at this time
if ($i == 7)
{
sscanf($data, "%s %s %s %s", $s7, $lat, $s8, $lon);
}
$hline = "$unit $mission $cycle $stage $lat $lon $date $time";
if ($i >= 8)
{
sscanf($data, "%s %s %s %s %s", $s9, $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
| |
| GreyWyvern 2006-08-30, 6:43 pm |
| And lo, Patrick didst speak in alt.www.webmaster:
> Hi All,
>
> Can someone (Grey?) take a look at my code and tell me if I am missing
> something
Sure, sure. I'm everyone's favorite proofreader[1] :P
> 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 could not confirm your results. The code, as given and executed on my
server, did not give me any duplicate lines. PHP 4.4.4 on Apache/Linux.
Is this all the code which is running at the time? Or did you extract it
from a larger script?
> I asked this over in a PHP group and only got one reply. He suggested
> that it was because the data file had an additional line at the end. I
> had already checked this.
I got the same result with and without an empty last line.
Sorry,
Grey
[1] I was originally going to say "debugger", but thought it might
encourage few unnecessary witty remarks :P
--
The technical axiom that nothing is impossible sinisterly implies the
pitfall corollary that nothing is ridiculous.
- http://www.greywyvern.com/orca#search - Orca Search: Full-featured
spider and site-search engine
| |
| Andy Dingley 2006-08-30, 6:43 pm |
|
Patrick wrote:
> Can someone (Grey?) take a look at my code 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.
What happens under PHP if you call fgets() when you're already at the
end of file ? Looks like you're reading the last line of the file but
feof() isn't true at this point and so it loops again. Another call to
fgets() returns null and does now set eof true. $data probably gets
returned as a null, and when sscanf() is given a null as input it
doesn't clear the variables it would normally write to.
Try finding some PHP example code on how to reliably read files --
probably testing for eof at the same point as the read, not through
three separate functions that have the potential to get unsynchronised.
OTOH, I despise PHP and have deliberately forgotten what little I ever
knew - so don't listen to me. 8-)
| |
| Patrick 2006-08-30, 6:43 pm |
| GreyWyvern wrote:
> And lo, Patrick didst speak in alt.www.webmaster:
>
>
>
> Sure, sure. I'm everyone's favorite proofreader[1] :P
>
>
>
> I could not confirm your results. The code, as given and executed on
> my server, did not give me any duplicate lines. PHP 4.4.4 on
> Apache/Linux.
>
> Sorry,
> Grey
Grey,
The problem with always giving the correct answers is that everyone is
going to think of you when they have a problem. Comes with the territory.
So with that being said, you hit it on the head once again. I ran the
code on our main web server and the extra line was not written. It
appears that the machine I was working on, where the data are stored,
has older versions running. Thanks again.
Patrick
--
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
| |
| GreyWyvern 2006-08-30, 6:43 pm |
| And lo, Patrick didst speak in alt.www.webmaster:
> The problem with always giving the correct answers is that everyone is=
=
> going to think of you when they have a problem. Comes with the territo=
ry.
Would you believe... 2 + 2 =3D 3? [1]
> So with that being said, you hit it on the head once again. I ran the =
=
> code on our main web server and the extra line was not written. It =
> appears that the machine I was working on, where the data are stored, =
=
> has older versions running. Thanks again.
Older versions of what, PHP? It's a moot point since the bug has =
apparently been fixed (whatever it was), but unrequested duplication of =
=
output would be quite a serious problem for any system. So I am a trifl=
e =
curious :)
Anyway, I'm just glad I could help out!
Grey
[1] For very large values of 3
-- =
The technical axiom that nothing is impossible sinisterly implies the =
pitfall corollary that nothing is ridiculous.
- http://www.greywyvern.com/orca#search - Orca Search: Full-featured =
spider and site-search engine
|
|
|
| | Copyright 2003 - 2008 forum4designers.com Software forum Computer Hardware reviews |
|