LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-27-2005, 01:34 PM   #1
mrobertson
Member
 
Registered: May 2005
Posts: 275

Rep: Reputation: 30
reading text from a file


I have the following code:

Code:
<?php

$filename = "C:\Camera Data\camdata.txt";

$myfile = fopen($filename, "r");

$fcontents = file($filename);

while(list ($line_num, $line) = each ($fcontents)) {
	echo "<p><b>Line $line_num:</b>" . str_replace("<p>", "", $line) . <br>\n;
}

fclose($myFile);

?>
When I try to run this script via apache 2 web server I get the following error:

Fatal error: Only variables can be passed by reference in C:\Program Files\Apache Group\Apache2\htdocs\PHP3Camera2.php on line 9

Does anyone see what the problem may be here. I am just trying to read each line of a text file.
 
Old 06-27-2005, 01:55 PM   #2
Matir
LQ Guru
 
Registered: Nov 2004
Location: San Jose, CA
Distribution: Debian, Arch
Posts: 8,507

Rep: Reputation: 128Reputation: 128
Well, you do seem to be missing some quotes around the <br> tag there. Other than that, I don't see a syntax error, but perhaps some logic errors. (The while loop and the list for each).
 
Old 06-27-2005, 02:15 PM   #3
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
When using file(), you don't need to fopen() it fist. Not sure if this causes the problem though.

Also, I'd prefer a loop like this instead:
Code:
foreach ($fcontents as $line_num => $line) {
    // ....
}
 
Old 06-27-2005, 02:29 PM   #4
Matir
LQ Guru
 
Registered: Nov 2004
Location: San Jose, CA
Distribution: Debian, Arch
Posts: 8,507

Rep: Reputation: 128Reputation: 128
Excellent point, Hko. I'm not sure how I missed that one. file() does not require a previous fopen(), and would probably not be fun.
 
Old 06-28-2005, 07:19 AM   #5
mrobertson
Member
 
Registered: May 2005
Posts: 275

Original Poster
Rep: Reputation: 30
Thank both of you very much for your help. I finally got the script working as I would like it to. I have one last question however.....My output looks as follows:


Line 0:The coil ID is 1325462


Line 1:The coil width is 45.000


Line 2:The coil footage count is 97


Line 3:The coil length is 536


How would I just print the sentence and not line number?
 
Old 06-28-2005, 07:38 AM   #6
Proud
Senior Member
 
Registered: Dec 2002
Location: England
Distribution: Used to use Mandrake/Mandriva
Posts: 2,794

Rep: Reputation: 116Reputation: 116
Remove
Code:
"<p><b>Line $line_num:</b>" .
from the echo line?
 
Old 06-28-2005, 08:03 AM   #7
mrobertson
Member
 
Registered: May 2005
Posts: 275

Original Poster
Rep: Reputation: 30
Thanks alot! It works perfectly!
 
Old 06-28-2005, 08:53 AM   #8
Matir
LQ Guru
 
Registered: Nov 2004
Location: San Jose, CA
Distribution: Debian, Arch
Posts: 8,507

Rep: Reputation: 128Reputation: 128
It now looks like you're just trying to print out a file, which could be done with this one-liner:
Code:
print str_replace("\n","<br>",file($filename));
But I could be wrong. Just a thought to keep in mind.
 
Old 06-28-2005, 08:55 AM   #9
mrobertson
Member
 
Registered: May 2005
Posts: 275

Original Poster
Rep: Reputation: 30
Thanks...the code that I have now works exactly how I wanted it.
 
Old 06-28-2005, 11:17 AM   #10
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
Just a side note

Quote:
Originally posted by Matir
It now looks like you're just trying to print out a file, which could be done with this one-liner:
Code:
print str_replace("\n","<br>",file($filename));
As a side note to this:
There even is a special PHP function to do this (nl2br()):
PHP Code:
print nl2br(file($filename)); 
 
Old 06-28-2005, 11:21 AM   #11
Matir
LQ Guru
 
Registered: Nov 2004
Location: San Jose, CA
Distribution: Debian, Arch
Posts: 8,507

Rep: Reputation: 128Reputation: 128
That is something I never knew, but is very cool.
 
Old 06-28-2005, 11:54 AM   #12
mrobertson
Member
 
Registered: May 2005
Posts: 275

Original Poster
Rep: Reputation: 30
what does that special function mean and do?
 
Old 06-28-2005, 12:00 PM   #13
Proud
Senior Member
 
Registered: Dec 2002
Location: England
Distribution: Used to use Mandrake/Mandriva
Posts: 2,794

Rep: Reputation: 116Reputation: 116
http://www.php.net/manual/en/function.nl2br.php
 
Old 06-28-2005, 12:00 PM   #14
Matir
LQ Guru
 
Registered: Nov 2004
Location: San Jose, CA
Distribution: Debian, Arch
Posts: 8,507

Rep: Reputation: 128Reputation: 128
As the manual says, it "Inserts HTML line breaks before all newlines in a string".
 
Old 06-28-2005, 12:01 PM   #15
Matir
LQ Guru
 
Registered: Nov 2004
Location: San Jose, CA
Distribution: Debian, Arch
Posts: 8,507

Rep: Reputation: 128Reputation: 128
LOL, nice work Proud.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Bash script - reading from text file twantrd Programming 4 11-24-2004 12:38 AM
C++ reading from text file query lrt2003 Programming 5 05-15-2004 04:25 AM
C help needed - reading from a complicated text file Keffin Programming 4 01-26-2004 09:32 PM
reading a text file and outputting to another. Hardw1re Programming 28 11-03-2003 08:51 AM
reading a single line of a text file davi_cabral Linux - Software 1 10-29-2003 12:24 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:24 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration