Page 1 of 1

Few problems with opening files and reading files

Posted: Mon Mar 31, 2008 8:08 am
by nadavgo
I have two problems :
using php 5.2.5
Hi there !!
please, help :)...
1. I wrote a function that opens a file and writing data to the file:
I wanted to write 1 value in each line but it didn't work., in the beginig i used:

$dataFile = fopen("./connect4.dat", 'w');

i read in a forum that a solution is to change to

$dataFile = fopen("./connect4.dat", 'at');

and it worked. BUT IS THERE ANOTHER SOLUTION?

2. I am trying to read from the same file with a function
but it takes a few minutes. i reduced the function to very simple (only for checking) - only get to the end of file, but it takes very long time...
what happened and how can i solve it?

function writeData($board) /* writing the game data to a file */
{
/* open a file */
$dataFile = fopen("./connect4.dat", 'at');

/* checking the file open was successful */
if (!$dataFile)
{
echo "Error writing to data file";
exit;
}

/* writing the data to file */
for ($rowCnt = 1; $rowCnt <= 6; $rowCnt++)
{
for ($colCnt = 1; $colCnt <= 6; $colCnt++)
{
$player = $board[$rowCnt][$colCnt] -> getPlayer();

fputs($dataFile, $player);
fputs($dataFile, "\n");
}
}

/* closing file*/
fclose($dataFile);
}


function readData ($board) /* reading the game data from the file */
{
/* open a file */
$dataFile = fopen("./connect4.dat", 'r');
/* checking the file open was successful */
if (!$dataFile)
{
echo "Error reading from data file....";
exit;
}

/* read in the data from the file */

While(!feof(!$dataFile))
{
}
}


Thank you, guys :)

Re: Few problems with opening files and reading files

Posted: Mon Mar 31, 2008 10:41 am
by Jonah Bron
It looks like this would be an opportunity to use XML or a Database.

Re: Few problems with opening files and reading files

Posted: Mon Mar 31, 2008 12:50 pm
by s.dot
You can use w+ instead of w.

Does getPlayer() execute a query or reading of another text file? If so, you do not want to do that inside of a loop. You will want to grab the information at once if possible, then pass your results for writing. You don't want to read while writing.

Re: Few problems with opening files and reading files

Posted: Tue Apr 01, 2008 1:43 am
by nadavgo
PHPyoungster wrote:It looks like this would be an opportunity to use XML or a Database.
in this case i am supposed to write to a file only 42 numbers (0,1 or 2) each one in a different line....
are you sure it is not reasonable for a file?

Anyway, in general, in what cases do you prefer to use a file?

Thanx

Re: Few problems with opening files and reading files

Posted: Tue Apr 01, 2008 1:49 am
by nadavgo
scottayy wrote:You can use w+ instead of w.

Does getPlayer() execute a query or reading of another text file? If so, you do not want to do that inside of a loop. You will want to grab the information at once if possible, then pass your results for writing. You don't want to read while writing.
i tried your suggetsion... but when i chose 2 open the file with w+ , it got the followin char: ਰ instead of 0 and a new line....

no reading from the file in the function getPlayer:

function getPlayer()
{
return($this->player);
}

any other ideas?

Thanx :)