- Home /
The question is answered, right answer was accepted
Loading data from a txt file - C#
...Continuation of this
Now I've created a text file with code... Now I just need to read the text i've written down in the .txt -how?
-thanks :)
EDIT: Fixed the link
I was looking to do similar things when I stumbled across these answers. Thank you. :-)
These are great and have helped as much as this article providing some good sample code to get started as well in case anyone else is looking too. What I like most about it is it's example incorporates parallel-processing (which wasn't as readily available in 2012 I think).
Answer by Drakestar · Jul 05, 2012 at 02:16 PM
Your link doesn't seem to work.
Generally, you use a streamreader to load in text. Here's some code (untested) that should get you on the right track:
using System.Text;
using System.IO;
private bool Load(string fileName)
{
// Handle any problems that might arise when reading the text
try
{
string line;
// Create a new StreamReader, tell it which file to read and what encoding the file
// was saved as
StreamReader theReader = new StreamReader(fileName, Encoding.Default);
// Immediately clean up the reader after this block of code is done.
// You generally use the "using" statement for potentially memory-intensive objects
// instead of relying on garbage collection.
// (Do not confuse this with the using directive for namespace at the
// beginning of a class!)
using (theReader)
{
// While there's lines left in the text file, do this:
do
{
line = theReader.ReadLine();
if (line != null)
{
// Do whatever you need to do with the text line, it's a string now
// In this example, I split it into arguments based on comma
// deliniators, then send that array to DoStuff()
string[] entries = line.Split(',');
if (entries.Length > 0)
DoStuff(entries);
}
}
while (line != null);
// Done reading, close the reader and return true to broadcast success
theReader.Close();
return true;
}
}
// If anything broke in the try block, we throw an exception with information
// on what didn't work
catch (Exception e)
{
Console.WriteLine("{0}\n", e.Message);
return false;
}
}
}
Do I need to import a package to do this?... And Also. I dont understand how this code works... $$anonymous$$aybe some comments would be nice? :) Also. I fixed the link
Added some comments and namespaces. I think these are all the libraries used in the code.
So. Where do I specify the line and the text document? (Lets say I have my document in the "C:/$$anonymous$$yGame/data/file.txt" directory, and the code I need is on the 5th line in the document)
The file name is in the first argument of the StreamReader constructor. It's a string with the path and filename. The reader reads in line after line from the document - to act on the 5th line you could do many things: create an array of strings and add each line in the do/while loop, and then access array[4], or simply put a counter in the loop that detects when you're on line 5.
By the way, since you're now working with reading/writing text files and encodings, it's probably time to read this article: "The Absolute $$anonymous$$inimum Every Software $$anonymous$$bsolutely, Positively $$anonymous$$ust $$anonymous$$now About Unicode and Character Sets (No Excuses!)" http://www.joelonsoftware.com/articles/Unicode.html
Answer by joncham · Jul 07, 2012 at 07:25 PM
Similar to the answer to the original question referenced, you could use the File.ReadAllText method:
string text = System.IO.File.ReadAllText("myfile.txt");
This only works in Unity Editor but not on the targeted platforms
Answer by JDyo2 · Jul 25, 2015 at 03:54 PM
It's better do
using (theReader)
{
line = theReader.ReadLine();
if(line != null){
// While there's lines left in the text file, do this:
do
{
// Do whatever you need to do with the text line, it's a string now
// In this example, I split it into arguments based on comma
// deliniators, then send that array to DoStuff()
string[] entries = line.Split(',');
if (entries.Length > 0)
DoStuff(entries);
line = theReader.ReadLine();
}
while (line != null);
}
// Done reading, close the reader and return true to broadcast success
theReader.Close();
return true;
}
}
It uses an if less inside the do-while
Follow this Question
Related Questions
C# Issues with either PlayerPrefs.SetVariable or UnityEvent.Invoke 1 Answer
create text file in C#. 2 Answers
How do I save and load the value of a text? 1 Answer
Trouble creating a text file 4 Answers
Filetype association 0 Answers