The question is answered, right answer was accepted
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]);
}
}
} }```
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
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
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.
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");
Alright thanks a lot, I'll try that out in a little while and let you know how it works out
@im$$anonymous$$4TT 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?
Do you tried something like this :
foreach(String item in dataValues){
strWriter.WriteLine(item);
}
I may miss something ?
Follow this Question
Related Questions
How can i save random-generated maps during gameplay ? 1 Answer
Problem saving with PlayerPrefs 0 Answers
Hi I got this error when looking for a streamReader file. Anyone can help? 0 Answers
PlayerPrefs help ! 1 Answer
Saveing and Loading Problem 0 Answers