|
-
June 27th, 2012, 02:45 PM
#3
Re: Reading properties file
If by anything you mean code for reading it, I was trying to think of how to go about it when a friend suggested I use ReadLine to put each line into a string array, then for every property I could loop through the array until the first few characters of a line match that property's name. Once I got the property's position in the array I could use array[index].Substring(#, #) to get the value, and Convert.ToInt or something to convert it to a numeric value. Could that work?
To do that for every single property (I'm expecting 10 - 60 properties per file) seems rather complex and expensive, but as you noted, it's a really odd file, so I'll have to do whatever works. To that end I wrote up some code to test it:
Code:
string fileName;
string[] fileDataArray;
int fDAIndex = 0;
private void loadToolStripMenuItem_Click(object sender, EventArgs e)
{
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
fileName = openFileDialog1.FileName;
System.IO.StreamReader tr = new System.IO.StreamReader(fileName);
// Read the lines of the file into an array
while (tr.Peek() >= 0)
{
fileDataArray[fDAIndex] = tr.ReadLine();
fDAIndex = fDAIndex + 1;
}
tr.Close();
fDAIndex = 0;
// Get the index where the string containing "Angle(-90.0);" is stored
while (fileDataArray[fDAIndex].Substring(0, 5) != "Angle")
{
fDAIndex = fDAIndex + 1;
}
// Get the "-90.0" part of the string
string angleValueStr = fileDataArray[fDAIndex].Substring(5, 5);
// To check if it worked, display angleValueStr in the angleValueText textbox (already on the form)
angleValueText.Text = angleValueStr;
}
}
When I load the file, I get an error at fileDataArray[fDAIndex] = tr.ReadLine(); saying that "NullReferenceException was unhandled" and "Object reference not set to an instance of an object." What's going on? Moreover, is there a better way to get this data than this current playing around with ReadLine and string array and substrings?
EDIT - I changed the code slightly. Instead of that bit with the tr StreamReader and the while loop that loads the lines into an array, I now just have the one line:
Code:
fileDataArray = System.IO.File.ReadAllLines(fileName);
I still get an error when loading the file, this time at the line that says
Code:
while (fileDataArray[fDAIndex].Substring(0, 5) != "Angle")
The error is "Index and length must refer to a location within the string." The problem is with Substring(0, 5); some lines only have one character, so I get an error when Substring tries to read the first 5 characters. How to I get the while loop to continue on past that error? I can't really do Substring(0, 1) since 1 character isn't really much use in identifying the property.
Further, even when I used Substring(0, 1) != "A", I got the same error. I guess C# reads the lines with just newline characters as characterless, so it will only accept Substring(0, 0)?
Last edited by fiodis; June 27th, 2012 at 06:19 PM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|