Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by hatzalex · Nov 22, 2011 at 03:33 PM · iosbuildtextreading

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
1

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!

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

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

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

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

Unity Build ios Xcode unity version missing error 0 Answers

Am i able to export to android with 30 day pro ? 3 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges