Few problems with opening files and reading files
Posted: Mon Mar 31, 2008 8:08 am
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
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