Sign in to follow this  
ranakor

C# string/stringreader help needed (how to read each line from a string?)

Recommended Posts

i'm having trouble figuring out how to read each line from a string i have a string created from a file (it contains the whole multi-line file content) but neither string nor stringreader seems to have a lines property or anything i could use to know when i'm done reading the last line (so i have no idear how i could use stringreader.readline to read all lines in the string since i don't know when i got to the last one & will get an exception) is there any standard way to do this or am i supposed to get the linecount from the file or watch for an exception or something?

Share this post


Link to post
Share on other sites
Edit I just reread your post what I did is add END to the end of my file and when I read that from the file I knew it was done. I also have seen people check to see if readLine() returns null.


Here is a sample that I did to load a list of waypoints to a file. This was a console app I made to test my class for a game I am making.


class Program
{

static void Main(string[] args)
{
Console.WriteLine("Loading Level");
Level lev = new Level();
}
}
class Level
{
Waypoints tmp;
ArrayList currentWaypoints;
StreamReader file;
public Level()
{
Console.WriteLine("In Level Constructor");
LoadLevel1();
}
private void LoadLevel1()
{
file = new StreamReader("level1.txt");
tmp = new Waypoints();
currentWaypoints = new ArrayList();
tmp.setLeft(8);
tmp.setRight(47);
currentWaypoints.Add(tmp);
string nextString = file.ReadLine();
Console.WriteLine(nextString);
if (nextString.Contains("Waypoint"))
{
while (nextString != "END")
{
tmp.setRight(99);
tmp.setLeft(99);
tmp.setDown(99);
tmp.setUp(99);
if (nextString.Contains("Vector"))
{
int x, y;
int beginningOfX = nextString.IndexOf('x')+1;
int beginningOfY = nextString.IndexOf('y', beginningOfX)+1;

x = Convert.ToInt32(nextString.Substring(beginningOfX, beginningOfY - beginningOfX - 1));
y = Convert.ToInt32(nextString.Substring(beginningOfY));
Console.Write("Position.x = ");
Console.WriteLine(x + 64);
Console.Write("Position.y = ");
Console.WriteLine(y + 64);
}
nextString = file.ReadLine();
int nextNode = 0;
while (!nextString.Contains("Waypoint") && nextString != "END")
{
if (nextString.Contains("Up"))
{
nextNode = Convert.ToInt32(nextString.Substring(3));
tmp.setUp(nextNode);
}
if (nextString.Contains("Down"))
{
nextNode = Convert.ToInt32(nextString.Substring(5));
tmp.setDown(nextNode);
}
if (nextString.Contains("Left"))
{
nextNode = Convert.ToInt32(nextString.Substring(5));
tmp.setLeft(nextNode);
}
if (nextString.Contains("Right"))
{
nextNode = Convert.ToInt32(nextString.Substring(6));
tmp.setRight(nextNode);

}
Console.WriteLine(nextNode);
nextString = file.ReadLine();
}
currentWaypoints.Add(tmp);
}
}//End while
file.Close();
Console.WriteLine("Total Size of ArrayList is");
Console.WriteLine(currentWaypoints.Count);
}
}
}




This is what a portion of my file looks like

Waypoint 1 Vector x17 y17
Right 2
Down 5
Waypoint 2 Vector x97 y17
Right 3
Down 4
Waypoint 3 Vector x129 y17
Left 2
Waypoint 4 Vector x97 y49
Right 38
Left 63
Waypoint 5 Vector x17 y81
Right 6
Down 7
Waypoint 6 Vector x65 y81
Up 63
Down 12

Share this post


Link to post
Share on other sites
thank you both (i'm mostly learning the basics so my books don't go that in depth & most of the api's function i get from intellisence & the comment on readline didn't specify it would return null, thanks for putting with me & the help ratings++inc)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this