| Blockland Forums > Modification Help |
| FileObjects - Saving and Loading Data |
| (1/4) > >> |
| Nexus:
Hi, and thanks for your responses ahead of time. I am new to fileObjects, and have been trying to make a function that can save strings of data, preceeded by an identifying number, and another function that can find a string by that identifying number. For example, if I wanted the text document to read as follows: --- Code: ---702 wharrgarbl 62 lemon pie 4901 taco bell mascot 7 new mexico --- End code --- How would I check to see if a line that started with 4901 already existed, so I could replace it with "4901 taco bell mascot" in case it read something different? This was my attempt at this: --- Code: ---function savedata(%id, %data) { %file = new fileobject(); %file.openforwrite("config/server/randomtest.txt"); while(!%file.isEOF()) { %line = %data.readline(); if(getword(%line, 0) $= %id) { echo("data found, overwriting..."); %file.writeline(%id TAB %data); %found = true; } } if(!%found) { echo("data does not exist! creating..."); %file.writeline(%id TAB %data); } %file.close(); %file.delete(); } --- End code --- However, this code just keeps overwriting the first line, and I'm not sure how to fix it, or where to start for the LoadData function that would go with it. |
| Kalphiter:
Wait, why are you trying to write yet are attempting you read at the same time? |
| Nexus:
--- Quote from: Kalphiter on April 10, 2011, 11:44:11 PM ---Wait, why are you trying to write yet are attempting you read at the same time? --- End quote --- I am attempting to find and replace a line that is identified by the first word on that line. |
| Iban:
Using an FO to manage data is a horrendous idea. |
| lilboarder32:
--- Quote from: Iban on April 11, 2011, 12:26:59 AM ---Using an FO to manage data is a horrendous idea. --- End quote --- This. Use ScriptObjects to store data. |
| Navigation |
| Message Index |
| Next page |