How to include .txt file on iphone!

This works beautifully when testing it on the computer. But the iphone isn’t including or recognizing my .txt file on the device. If any way possible, I don’t want to change my stream reader. Hoping I can just include a path. Here is my code:

void ParseDict()
    {
        string path = @"Assets/Dictionary.txt";
		
        
        try
        {
            // Create an instance of StreamReader to read from a file.
            // The using statement also closes the StreamReader.
            using (StreamReader sr = new StreamReader(path.ToString()))
            {
                string line;

                // Read and display lines from the file until the end of
                // the file is reached.

                while ((line = sr.ReadLine()) != null)
                {
                    dictionary.Add(line, line);
                }
            }
        }
        catch (System.Exception e)
        {
            // Let the user know what went wrong.
            UnityEngine.Debug.Log("The file could not be read:");
            UnityEngine.Debug.Log(e.Message);
        }

      

    }

I saw a post that said this:

"/<AppName.app>/Data

You just need to concatenate a slash and the name of the file to the end of the string returned by Application.dataPath.

Are you aware of the TextAsset class?

I tried using
string path = TextAsset.text , but that didn’t work.

This doesn’t work either:

string path = Application.dataPath.Substring (0, Application.dataPath.Length - 5); 
        // Strip application name 
        path = path.Substring(0, path.LastIndexOf('/'));  
        path += "/Documents/Dictionary.txt";

if you drop a txt into the project it will be treated as text asset and integrated in the generated bundle. it will no longer exist as file.

as smag mentioned, use the TextAsset class to get the content of the txt for further processing.

Yes, but how do I read lines from a TextAsset with my Streamreader??

You don’t need a streamreader, you read it by doing textAsset.text

But i’m trying to read each line in the text file into a c# Dictionary - 1 line per dictionary value. I’m not sure how to do that without streamreader

String.Split is handy for this stuff.

its c# syntax should be pretty normal
its javascript syntax is a bit funky though

JS:
var strArr : String[ ] = someString.Split(“\r”[0]);

will return 1 array entry per line (assuming you use a CR line-ending)

look up String.Split() in the monodocs for more info.

Also warning: I ran into some issues with text using CRLF line endings when porting from PC Unity2.6 to iPhone1.x

either that or read out the full text and then write it out to the projects cache folder and read it from there like a regular file again.

Ok, so after many hours of frustration and trial and error I fixed the problem.

I used this to feed into the stream reader:
string path = Application.dataPath + “/Dictionary.txt”;

I then built the game in unity. I then physically copied the file Dictionary.txt into the “Data” folder in the Unity game build folder. I then Built and Ran from xcode. Works now.

The revised code:

void ParseDict()
    {
        
		string path = Application.dataPath + "/Dictionary.txt";
		
        try
        {
            // Create an instance of StreamReader to read from a file.
            // The using statement also closes the StreamReader.
            using (StreamReader sr = new StreamReader(path))
            {
                string line;

                // Read and display lines from the file until the end of
                // the file is reached.

                while ((line = sr.ReadLine()) != null)
                {
                    dictionary.Add(line, line);
                }
            }
        }
        catch (System.Exception e)
        {
            // Let the user know what went wrong.
            UnityEngine.Debug.Log("The file could not be read:");
            UnityEngine.Debug.Log(e.Message);
        }


    }

Cool this would be awesome if I could only get it working…

Cheers and thanks for the share.

Any solution to get this done automatically?

Ah, sometimes the needed information is hard to find in the docs…
So, to get a specific file I would write

path = Application.dataPath + “/Raw/MyPointCache.pc2”;

Right?

Thanks!

smth like that, yes
or you can continue digging docs and find this

:wink:

“An example of this is the deployment of a movie file on iOS devices”

I thought movies on iOS don´t work that easy - and indeed I get error while compiling:
“The name “MovieTexture” does not denote a valid type…”