How do I use fileIO to write a string to file?
Write
Code:
SetSave("filename"); //This can be a string instead, rather than a literal
WriteString("information"); //This can be a string instead, rather than a literal
Be sure to use SetSave before performing any writing actions (not for every one - just at the start).
How do I use fileIO to get a string from file?
Write
Code:
SetLoad("filename"); //This can be a string instead, rather than a literal
new Find = FindStringStart("name of string"); //Other arguments are set to default
if(Find == -1)
{
ReplyToCommand(client,"Could not find your string");
return;
}
new String:buffer[256]; //Doesn't have to be this big so long as you know the string definitely won't exceed the storage
ReadString(buffer,Find); //Reads in the string from the given position
ReplyToCommand(client,"The string is %s",buffer);
How would I read information between two strings?
Tricky, but relatively straight-forward
Code:
SetLoad("filename");
new Start = FindStringEnd("first");
if(Start == -1){return false;} //Didn't find it
new End = FindStringStart("last");
if(End == -1){return false;} //Didn't find it
new String:buffer[256]; //Again, doesn't have to be this big
//CS is short for 'count string' - which is what ReadString returns
new CS = 0;
for(;CS != -1;)
{
CS = ReadString(buffer,Start); //Note there is a third variable, that if set to true, removes the newline - useful for string comparisons!
if(CS == -1)
{
return false; //Error reading the string
}
Start = Start + CS;
ReplyToCommand(client,"The string is %s",buffer);
if(Start > End)
{
return true; //We read all the strings in that position
}
}
return false; //We hit an unknown error
How do I remove specific lines or strings from file?
Depends. If you know which lines, you would do...
Code:
new arr[5]
arr[0] = 4;
arr[1] = 3;
arr[2] = 10;
arr[3] = 11;
arr[4] = -1;//The last one in the array is always -1 so DeleteLines knows it's reached the end.
SetFile("filename");
DeleteLines(arr);
If you don't know the line numbers though, fear not!
FileIO has it's own function for that does it for you...
Code:
DeleteString(guessed_name); //This can be as precise or as vague a target as you want, however, be wary, as it will delete the first match.
How do I write an article to file?
This is straight-forward. You'll need the name, and the items to be put in.
Note, items have to be of the same datatype in order to be written - this is to ensure that fileIO knows what kind of data to expect, and reduces potential errors.
If you have mixed types (int/floats), use the SaveArticleFloat, and write the integer as a float, then perform the correct extraction process yourself when reloading the data.
To Write an Article, you would write...
Code:
new arr[4]
arr[0] = kills;
arr[1] = deaths;
arr[2] = times_peed_the_bed;
arr[3] = your_house_number;
SetSave("filename");
SaveArticleInt("I know where you live",arr,4);
And uuh... done. Thats it.
How do I read an Article from file?
Assuming you maintain consistency on how you write information to file, you extract it pretty much the same way.
Because not always people know where the article is, whether or not it exists or the name it's under (rule of Entropic decay), the LoadArticle functions are designed to search with a vague guess of a title with the expectancy of finding the right title when searching.
The last argument, P (stands for Position) takes the Position in file, so if you already know where it isn't, you can make it look for it where it most likely is.
Don't worry if there is a string (a non-article) that has the same name; LoadArticle functions won't try to read information if the next line contains a letter character. It won't resume search in-case there has been a serious error (like incorrect appending of strings after an Article Title etc).
Code:
new arr[4];
new String:full_title[128]
new String:guessed_title[64]
guessed_title = "where you live";
SetLoad("filename");
new N = LoadArticleInt(guessed_name,full_title,arr,4,0); //0 specifies the start
if(N == -1){return false;} //We didn't find the article
ReplyToCommand(client,"You earnt %d kills",arr[0]);
ReplyToCommand(client,"You suffered %d deaths",arr[1]);
ReplyToCommand(client,"You peed the bed %d times",arr[2]);
ReplyToCommand(client,"And your house number is %d",arr[3]);
ReplyToCommand(client,"Hey there, %s",full_title);
Thats pretty much all you need to know.