Jump to content

Rewriting data to .txt file


Mindaugas

Recommended Posts

Hello all, i know that database is better with MySQL , but i am working with my first simple project, and using .txt datadase.

 

And i won`t give up till this project will be done, then i`ll learn MySQL.

 

This is the question, is there any way to rewrite data to .txt file without overwriting all variables ?

 

for example, i`m creating simple game, and when player buying for example a weapon, his atk should add +x, and i have to overwrite all variables like this.

 
//deleting old info
$filees = fopen("krepsys/$vartotojas.txt","w");
fclose($filees);

//rewriting variables and weapons power
$inf2[3] = $weaponsPower;

$duoma = $inf2[0] . ".." . $inf2[1] . ".." . $inf2[2] . ".." . $inf2[3] . ".." . $inf2[4] . ".." . $inf2[5] . ".." . $inf2[6] . ".." . $inf2[7] . ".." . $inf2[8] . ".." . $inf2[9] . ".." . $inf2[10] . ".." . $inf2[11] . ".." . $inf2[12] . ".." . $inf2[13] . ".." . $inf2[14] . ".." . $inf2[15] . ".." . $inf2[16] . ".." . $inf2[17] . ".." . $inf2[18] . ".." . $inf2[19] . ".." . $inf2[20] . ".." . $inf2[21] . ".." . $inf2[22] . ".." . $inf2[23] . ".." . $inf2[24] . ".." . $inf2[25] . ".." . $inf2[26] . ".." . $inf2[27] . ".." . $inf2[28] . ".." . $inf2[29] . ".." . $inf2[28] . ".." . $inf2[29] . ".." . $inf2[30] . ".." . $inf2[31] . ".." . $inf2[32] . ".." . $inf2[33] . ".." . $inf2[34] . ".." . $inf2[35] . ".." . $inf2[36] . ".." . $inf2[37] . ".." . $inf2[38] . ".." . $inf2[39] . ".." . $inf2[40] . ".." . $inf2[41] . ".." . $inf2[42] . ".." . $inf2[43] . ".." . $inf2[44] . ".." . $inf2[45] . ".." . $inf2[46] . ".." . $inf2[47] . ".." . $inf2[48] . ".." . $inf2[49] . ".." . $inf2[50] . ".." . $inf2[51] . ".." . $inf2[52] . ".." . $inf2[53] . ".." . $inf2[54] . ".." . $inf2[55] . ".." . $inf2[56] . ".." . $inf2[57] . ".." . $inf2[58] . ".." . $inf2[59];

  $failas11 = fopen("krepsys/$vartotojas.txt","a");
  fwrite($failas11, "$duoma");
  fclose($failas11);

 

Is any other way to overwrite only one file ?

I tried use only one variable, but then all others becoming empty :P

 

Link to comment
https://forums.phpfreaks.com/topic/187472-rewriting-data-to-txt-file/
Share on other sites

Seeing as the data is known to you,

 

$string = file_get_contents("file.txt");

list($a,$b,$c,$d)=explode("..","$string");

$b=$weaponsPower;

$NewString="$a..$b..$c..$d";

file_put_contents("$NewString","file.txt");

 

 

 

 

HTH

Teamatomic

 

thanks a lot ;)

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.