473,386 Members | 1,706 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

last line of data written twice

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
Aug 29 '06 #1
1 1309
*** 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
--
Aug 29 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Charles Erwin | last post by:
Is there any way, upon scanning in a file line by line to avoid missing the last line if there is not a newline character (aka you have to hit return on the last line of input in your file). I was...
16
by: DJP | last post by:
Hi, I need to read a file programmatically until end of file. My logic is as follows: while(!feof(Fp)) { fgets(readLine,10000,Fp);
35
by: RyanS09 | last post by:
Hello- I am trying to write a snippet which will open a text file with an integer on each line. I would like to read the last integer in the file. I am currently using: file = fopen("f.txt",...
3
by: seven.reeds | last post by:
Hi, I am running php v 5.0.0 on a redhat box. I have a php script in a file called p_script_1. I have another small script in file p_script_2. p_script_1 calles p_script_2 through a system()...
12
by: Curten | last post by:
Hi, I'm using ifstream to read data from a txt-file but the last line is always read twice. How Come? The condition for the loop in which the file is read is while(!infile.eof()) so this...
4
by: Rasputin | last post by:
Hello, The topic of this question is somewhat related to my previous question, but I wasn't sure if it diserved its own thread. In case of doubt... I double threated. If it's not OK let me...
2
by: subramanian100in | last post by:
Is my following understanding correct ? Data abstraction means providing the interface - that is, the set of functions that can be called by the user of a class. Information hiding means...
7
by: ramana | last post by:
I'm wondering if someone could point me to the flaw in the following code that uses the while(!FP.eof()) condition to read the input data. This condition is reading the last data point of the file...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.