Welcome to WebmasterWorld Guest from 184.72.177.182

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

Reading a text file into an array

     
9:04 pm on Sep 22, 2002 (gmt 0)

Full Member

10+ Year Member

joined:Aug 2, 2002
posts:212
votes: 0


I'm trying to read a text file (134 KB, 3500 lines) into an array using this code:

$fp = fopen("myfile.txt", "r");
$temp = fread ($fp, filesize ("myfile.txt"));
fclose ($fp);
$datalines = explode ("\n", $temp);

However the elements of the $datalines array are empty, i.e. they do not contain the lines of the file.
Before doing this I tried:

$datalines = file ("myfile.txt");

getting an array with 3500 empty elements. What can I do? Needless to say, I'm quite frustrated. In any major programming language you can declare an array of a predefined size, it exists and it is able to store what it has to store.

I might add that in a second step I want to sort the lines of this array using a sort function (will probably use a simple bubblesort). So I really need the array data structure otherwise the sorting gets quite difficult.

By the way, my provider has PHP 4.2.

9:29 pm on Sept 22, 2002 (gmt 0)

Administrator

WebmasterWorld Administrator jatar_k is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:July 24, 2001
posts:15755
votes: 0


>>In any major programming language you can declare an array of a predefined size

not if you mess it up ;)

Have you verified the data to ensure that everything is OK with text file? When you wrote these random lines of text to the txt did you write a newline char to the end of each line? It seems to know how many lines are there but doesn't get the data into it.

Have you tried a preg_split instead to see if that works?

11:05 pm on Sept 22, 2002 (gmt 0)

Full Member

10+ Year Member

joined:Aug 2, 2002
posts:212
votes: 0


The file contains newlines, so that shouldn't be the problem. I'll tr out the pregsplit and see if it works.

Is there any way to declare an array of a predefined size in PHP?

3:00 am on Sept 23, 2002 (gmt 0)

Full Member

10+ Year Member

joined:Aug 2, 2002
posts:212
votes: 0


preg_split didn't work, but I tried this:

$datalines = file ("myfile.txt");

foreach ($datalines as $zz) {
echo $zz . "<br>"; }

for ($i = 0; $i <= 10; $i++) {
echo $i . ": " . $datalines[i] . "<br>"; }

The foreach thing correctly echoed all lines of the file, but the for loop just echoed empty lines.

It seems to be an indexing or key problem. What key do I need to use to access the elements of the array? An integer from 0 to number of elements-1 or what else?

3:31 am on Sept 23, 2002 (gmt 0)

Administrator

WebmasterWorld Administrator jatar_k is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:July 24, 2001
posts:15755
votes: 0


>>echo $i . ": " . $datalines[i] . "<br>"; }

should be

echo $i . ": " . $datalines[$i] . "<br>"; }

I hope that's the problem because there is no reason why it shouldn't work.

9:36 am on Sept 23, 2002 (gmt 0)

Full Member

10+ Year Member

joined:Aug 2, 2002
posts:212
votes: 0


Thanks, that fixed the problem. I can't believe I lost 24 hours on such a simple error.

Since something like "$datalines[i]" is always incorrect, why doesn't the PHP parser generate an error report?

12:03 pm on Sept 23, 2002 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:July 22, 2002
posts:1782
votes: 0


Since something like "$datalines[i]" is always incorrect, why doesn't the PHP parser generate an error report?

It will issue a notice if you enable error reporting with

error_reporting(E_ALL);

SpY_MoBiLe

6:36 am on Oct 3, 2002 (gmt 0)

Inactive Member
Account Expired

 
 


I'm trying to do a similar thing: open a text file into a <textarea> tag so you can modify the contents and then save it overwriting the old text file. now i know how to save the contents once it has been placed in the <textarea>, but I'm still not sure on what code is used to insert the contents of the text file into the page. Thank you.
4:40 pm on Oct 3, 2002 (gmt 0)

Administrator

WebmasterWorld Administrator jatar_k is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:July 24, 2001
posts:15755
votes: 0


Welcome to WebmasterWorld [webmasterworld.com] SpY_MoBiLe

All you have to do is use fopen [php.net] to open the text file, use fread to read the contents into a var and then echo the var between the textarea tags. Then do what you need to when it's submitted.