• 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 /
  • Help Room /
This question was closed Jul 15, 2020 at 07:24 PM by rstrong for the following reason:

The question is answered, right answer was accepted

avatar image
Question by rstrong · Jul 14, 2020 at 11:48 PM · savingsave datadata storagecsvstreamreader

Trying to save to CSV file

I'm trying to work with a CSV file that is essentially a database of records. On load of the game I Read in a record and instantiate a prefab button one at a time with the correct text from the CSV file. It's not always the case that the index of the button will be the same as the index of the record where I got the information in the CSV file. So far I setup a Save function that I want certain values that have changed based on a tricks "name" field to reflect in the CSV file. Let me know what would be an easy way accomplish this. Here is the code for my Save function: ```void WriteCSVFile(){

  StreamReader strReader = new StreamReader("Assets/Resources/characters.csv.txt");
  StreamWriter strWriter = new StreamWriter("Assets/Resources/characters.csv.txt");
  
  bool endOfFile = false;
  string header = strReader.ReadLine();
  strWriter.WriteLine(header);
  string data;
  
  while(!endOfFile){
      data = strReader.ReadLine();
      strWriter.WriteLine(data);
      
      if(data == null){
          endOfFile = true;
          break;
      }
      
      //dataValues is each row
      var dataValues = data.Split(',');
      
      if(dataValues[1] == trickName){
          
          dataValues[3] = currentXP.ToString();
          dataValues[4] = currentLevel.ToString();
          dataValues[5] = maximumXP.ToString();
          strWriter.WriteLine(dataValues[3]);
          strWriter.WriteLine(dataValues[4]);
          strWriter.WriteLine(dataValues[5]);
          
      }
  }

} }```

Comment

People who like this

0 Show 1
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 Bunny83 · Jul 15, 2020 at 09:12 PM 0
Share

I reverted your question to the last sensible state. Please do not edit your actual question tile away and also keep the original question content. What's the point of having a question that is't a question anymore. Please note that UnityAnswers is not a private, free help system. UnityAnswers is a knowledge database for the whole community.


I would recommend you have a look at the FAQs

1 Reply

  • Sort: 
avatar image
Best Answer

Answer by imM4TT · Jul 15, 2020 at 12:23 AM

I think it's not possible to read and write at the same time as you said at the end of the movie refering to the ioexception.
You should first read and close the stream therefore write on the file.
Hope it helps

Comment
rstrong

People who like this

1 Show 6 · 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 rstrong · Jul 15, 2020 at 01:12 AM 0
Share

Yeah I assumed from the error that it's not possible to read and write to the same file at the same time. Do you think I should make an array to hold all the data being read and then use that array to write over that file? Or I could setup a temp file to write over the original file at the end of the save, which I was hoping I didn't have to do that because it seems like a lot of reading and writing to just change a few values of one record.

avatar image imM4TT rstrong · Jul 15, 2020 at 01:30 AM 1
Share

Yeah I would have proceeded as you described in first (read & store text file, then write on the same file) You could use this to read the whole file at once

string txt = System.IO.File.ReadAllText("myfile.csv.txt");

avatar image rstrong imM4TT · Jul 15, 2020 at 01:45 AM 0
Share

Alright thanks a lot, I'll try that out in a little while and let you know how it works out

Show more comments
avatar image rstrong · Jul 15, 2020 at 03:28 PM 0
Share

@imM4TT I need help with one more step. In the updated version of my save function you can see that I split each record by the commas into an array called dataValues. I make changes to the right fields in dataValues but now the text is separated into single lines when I do a Log statement. How do you think I can append the file back together to Write over the original CSV file?

avatar image imM4TT rstrong · Jul 15, 2020 at 05:14 PM 0
Share

Do you tried something like this :

 foreach(String item in dataValues){
 strWriter.WriteLine(item);
 }

I may miss something ?

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

207 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Some help with Data Save/Load would be most appreciated 3 Answers

Why is the BinaryFormatter's output human-readable? 0 Answers

Save multiple inputs for one item and print out all items 0 Answers

How can i save random-generated maps during gameplay ? 1 Answer

Problem saving with PlayerPrefs 0 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