Read random line of external text file data into a variable (JS)

So, what I want to do is this:
Read data from a file “data.txt”. I want to then pick a random line of that data, say line 3. At the moment, I’m reading the whole file. Can I just read line 3 of that file?

var fileName = “data.txt”;

// read from filename
var sr = new StreamReader(Application.dataPath + "/" + fileName);
var fileContents = sr.ReadToEnd();
sr.Close();

// put data from filename into a variable
    // var mydata = fileContents.Split("

"[0]);
var mydata = fileContents;

    // pick a random number between 1 and 10
    var myrandom = Random.Range(1,10);

// I know this bit isn’t right, but what I want is
print (line myrandom of mydata);

It’s in Javascript as that’s more human-readable for me. I just can’t get my head around C.
Also, sorry I’m really new to all of this. I don’t even know how to use tags on this website to show quoted code! :frowning: Please help!!

This does the job, quite good. It’s tested. (Excuse my poor JS) - Just give it the file path, and the line number that you want. This does it without reading the whole file contents, and then picking up your line. I also wrote a GetNumberOfLines for convenience. Don’t forget to include System.IO;

NOTE: if you want the first line of your file, you pass it a 0, for nLine, second, 1, etc - Just like indexing an array.

	[C#]
	string ReadLine(string path, int nLine)
	{
		string line = "";
		using (var reader = new StreamReader(File.Open(path, FileMode.Open))) {
		   int n = 0;
		   while (n++ <= nLine)
			   line = reader.ReadLine();
		}
		return line;
	}

	int GetNumberOfLines(string path)
	{
		int nLines = 0;
		using (var reader = new StreamReader(File.Open(path, FileMode.Open))) {
		    nLines = reader.ReadToEnd().Split('

').Length;
}
return nLines;
}

	[JS]
	function ReadLine(path : String, nLine : int) : String
	{
		var reader = new StreamReader(File.Open(path, FileMode.Open));
		var line : String = "";
		int n = 0;
		while (n++ <= nLine)
			line = reader.ReadLine();
		reader.Close();
		return line;
	}

	function GetNumberOfLines(path : String) : int
	{
		var reader = new StreamReader(File.Open(path, FileMode.Open));
		var nLines = reader.ReadToEnd().Split("

"[0]).Length;
reader.Close();
return nLines;
}

Test #1: Reads all the file, line by line

	var path = @"somewhere";
	var nLines = GetNumberOfLines(path);
	for (var i = 0; i < nLines; i++) {
	     print (ReadLine(path, i));
	}

Test #2: Reads a random line in the file

	var path = @"somewhere";
	var nLines = GetNumberOfLines(path); 
	var rand = Random.Range(0, nLines);
	var randomLine = ReadLine(path, rand);
	print (randomLine);

Almost there, but you commented out a line you really needed

import System.IO;
var fileName = "data.txt";
 
function Start () {

 	// read from filename
    var sr = new StreamReader(Application.dataPath + "/" + fileName);
    var fileContents = sr.ReadToEnd();
    sr.Close();
 
 	// put data from filename into a variable
    var mydata = fileContents.Split("

"[0]);

	// pick a random number between 1 and 10
	var myrandom = Random.Range(1,10);
	print(mydata[myrandom]);
}

Important to make sure you have import System.IO there as well. Also, its convention to start indexing arrays at 0 so you might want to change the Random.Range to between 0 and 9.