Reading generated text file

So I generated a text file with some keyboard inputs. It looks like this:

-inf name:SongName
-inf genre:SongGenre
-k 1|0.21
-k 1|0.7
-k 1|0.92
-k 1|1.04
-k 2|1.39
-k 1|1.64

Writing is easy but I need a little help with reading it. I know how to read and write to files using System.IO so that’s out of the question. The problem comes with interpreting the content.

What I basically want is to have several information variables (two of them shown here) which get the content from the lines marked with “-inf”. So when I launch the song, variable genre becomes “SongGenre” and variable name becomes “SongName”.

I can use the row number and always write the different information on specific rows but that’s kinda weak. I need the game to be able to actually read all the -inf lines and tell which one is what, then set variables.

Then the “-k” lines. These are the keyboard input records. On the left side is the key, on the right side is the time of the song during which the key has been pressed.
I need the code to be able to read all the keyframes and produce the key string in the right time.

Can you help me with any of these ? Help will me really really appreciated !

I think what you’re looking for is String.Split

the format is

var splitString : String[]; // create an array to receive the split up file

splitString = sourceString.Split("|",[0]); // the pipe in the quotes is the separator used to split up the string into the array

I’ve successfully used this save all my level data into a single file. Each level was separated by a comma, and then each different bit of level info was separated by a semi-colon.

Course it did take two separate split commands to get it working, but it’s a pretty powerful method.

Thanks for the fast reply!
Of course, I was thinking about Split for the | delimiter but I think I’m kinda far away from using it yet.
Hey, maybe if there is some way for returning all the lines from the file in a string array, everything would be really easy. However, I don’t think StreamReader has such function. It has ReadToEnd which returns a String value.

I don’t know enough about StreamReader to give a good answer to that.

But in my game I have a lot of separate variables that comprise the level data. To fill those vars, I have one big function that sets each var based on the data in the split level array. Not the most efficient, but it does get the job done.

It also could have been done better with a multi-dimensional array, but when I originally wrote it, Unity didn’t have any support for multi-dim arrays

Splitting by newline will return every line of the file in an array.

string[] datas = fileSource.Split(System.Environment.NewLine);
foreach (string data in datas)
{
    try
    {
        if (data.StartsWith("-inf")
        {

        }
        else if (data.StartsWith("-k")
        {

        }
    }
    catch (ArgumentNullException e)
    {

    }
}

Sweet! That’s what I needed, but that wouldn’t work in JavaScript, would it?
Split(System.Environment.NewLine);

Hey, that’s nifty!

I think if you did it like this, it should work in JS

datas : String[] = fileSource.Split(System.Environment.NewLine);
for (data : String in datas)
{

        if (data.StartsWith("-inf")
        {

        }
        else if (data.StartsWith("-k")
        {

        }


  }

I was hoping it was going to work but it didn’t. Here’s my code:

function ReadSongInfo(name : String){

var sr : StreamReader = new StreamReader(name+".txt");

var fullText : String = sr.ReadToEnd();

var datas : String[] = fullText.Split(System.Environment.NewLine);

}

That’s what the console says.

I thought it wasn’t going to work because I’ve only seen Split used with delimiters used as string values. So how can I tell Split I want it to split by line? Can I use “\n” or “\r” as a delimiter or something?

I’m asking if I can and not trying it out because right now I have some trouble on my head.

You can look up String.Split on MSDN to see how it works. As it happens, you can’t use System.Environment.NewLine like that since it’s a string rather than a char, but the simplest thing is to use System.Environment.NewLine.ToCharArray().

–Eric

Theres no Split method that takes a string as it says. Try one of these:

aString.Split(System.Environment.NewLine.ToCharArray());
aString.Split(new string[]{System.Environment.NewLine}, System.StringSplitOptions.None);

Though shalt RTFM :stuck_out_tongue:

EDIT: Oops beaten to the punch.

Boh - missed that, sorry. :slight_smile:

As a final note to feed off JohnnyA. Adding System.StringSplitOptions.RemoveEmptyEntries as the second argument would reduce the need for a try-catch block.

Sweet! Thank you all very very much for all the helpful replies! Thanks to you, I managed to make it work!

And just for the record, I RTFM, I always do that before asking or searching, it’s just that sometimes I get a little confused :slight_smile:

Anyway, thanks again and have an awesome new year!