Few problems with opening files and reading files

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
nadavgo
Forum Newbie
Posts: 3
Joined: Mon Mar 31, 2008 7:48 am

Few problems with opening files and reading files

Post 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 :)
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: Few problems with opening files and reading files

Post by Jonah Bron »

It looks like this would be an opportunity to use XML or a Database.
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Re: Few problems with opening files and reading files

Post 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.
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
nadavgo
Forum Newbie
Posts: 3
Joined: Mon Mar 31, 2008 7:48 am

Re: Few problems with opening files and reading files

Post 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
nadavgo
Forum Newbie
Posts: 3
Joined: Mon Mar 31, 2008 7:48 am

Re: Few problems with opening files and reading files

Post 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 :)
Post Reply