x


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.

more ▼

asked Nov 22 '11 at 03:33 PM

hatzalex gravatar image

hatzalex
61 7 9 10

(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

If you call Load before append the file you are looking for has not been created yet.

more ▼

answered Feb 19 '12 at 11:19 AM

StephanK gravatar image

StephanK
6k 39 53 93

(comments are locked)
10|3000 characters needed characters left

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!

more ▼

answered Apr 12 at 02:46 PM

frost86 gravatar image

frost86
1

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x1952
x652
x550
x10
x8

asked: Nov 22 '11 at 03:33 PM

Seen: 1351 times

Last Updated: Apr 12 at 02:46 PM