- Home /
Reading & Writing to text file After building for iPhone.
Hallo everybody,
I have encountered a problem when i want to read & write from a text file after i build the project for iOS. Before the build everything works fine. After the build the game is not able to read the text file. In Xcode i get the following error.
Could not find file "/var/mobile/Applications/00D2CD16-80C4-4E49-8632-58F62A3A8B00/BottleRoulette.app/Data/words_english.txt".
I assume this is a temporary file made for the build. This is the code I am using.
i mport System.IO;
var sr : StreamReader;
var sw : StreamWriter;
var wordArray : String[];
var currentWord :String;
var maxAmount : int = 30;
var currentAmount : int = 0;
var fileName : String = "words_english.txt";
function Start () {
print(Application.dataPath);
}
function changeWord () {
var randomInt = Random.Range(0,currentAmount);
currentWord = wordArray[randomInt];
guiText.material.color = Color.red;
guiText.text = currentWord;
}
function LoadFile () {
try {
// Create an instance of StreamReader to read from a file.
sr = new StreamReader(Application.dataPath + "/" + fileName);
// Count amount of words in file
currentAmount = 0;
while (sr.ReadLine() != null && currentAmount < maxAmount) {
currentAmount++;
}
Debug.Log("Size is:" + currentAmount.ToString());
wordArray = new String[currentAmount];
sr = new StreamReader(Application.dataPath + "/" + fileName);
// Fill array with words from file
var i : int = 0;
var line : String = sr.ReadLine();
while (line != null && i < maxAmount) {
wordArray[i] = line;
Debug.Log("Added word: " + line);
line = sr.ReadLine();
i++;
}
sr.Close();
}
catch (e) {
// Let the user know what went wrong.
print("The file could not be read:");
print(e.Message);
}
}
function AddToFile (word : String) {
try {
// Create an instance of StreamWriter to write text to a file.
sw = new StreamWriter(Application.dataPath + "/" + fileName);
// Add some text to the file.
//sw.Write("The date is: ");
sw.WriteLine(word);
sw.Close();
Debug.Log("I can access this");
}
catch (e) {
// Let the user know what went wrong.
print("File coudn't be accessed:");
print(e.Message);
}
LoadFile();
}
I also tried to code a hard path & use persistentDataPath with no success. Anyone knows how this is done properly?
Thanks in advance.
Answer by frost86 · Apr 12, 2013 at 02:46 PM
What is your full path to the file you want to modify? I spend several hours to solve this problem, so this is the way I've solved it: The first - use Application.persistentDataPath instead of Application.dataPath. And the second - I was trying to modify Application.persistentDataPath/Resources/file.txt and got the same error, then I put my file to the root folder Application.persistentDataPath/file.txt and write operation succeeded. Tested on Mac, Windows and iOS. Hope it helps!
Answer by StephanK · Feb 19, 2012 at 11:19 AM
If you call Load before append the file you are looking for has not been created yet.
Your answer
Follow this Question
Related Questions
iOS and Android reading/writing files 1 Answer
Distribute terrain in zones 3 Answers
UI Text does not show up on IOS build. 0 Answers