GetFileContents()
From Hack Wars Wiki
Code
NOTE: The 1st example is very inefficient. The 3rd one is pointless, it just wastes ops and does what the 2nd does afterwards. Just use the 2nd example within your code, instead of making a function for it.
This is a very simple function for looping a files contents to a string array. Enjoy, :-D..
file: name of the file which will be read data: the variable where the content of file will be stored
Known issues: If the file contains more lines than the maximum limit of an array (128 elements) then the script will abort.
string data[]; int length = countLines(file); for (int i=0; i <= length; i++){ data[i]= readLine(file, i); } |
A simpler version.
string data[] = split(readFile(file),char(10)); |
The above snippets cannot be easily used as functions because it is not possible to return arrays. However a workaround is possible by returning a string.
string getFileContents(string file) { int length = countLines(file); for (int i=0; i <= length; i++){ data[i]= readLine(file, i); } return join(data,char(10)); //change into a string } int main(){ string file = "test.txt"; string data[] = split(getFileContents(file),char(10)); //change back into an array } |
