Is it true there is no text parsing in C#?
-
I just read an article on parsing in c# and they said that unlike older languages, parsing in c# in simple terms, for a beginner, can be a nightmare. All I want to do is store room descriptors in a text file with a dillimeter and a value. - The first value is used to find the correct line to print. e.g 01 - The second part being the name of the room, or unless its the rooms look value, e.g 011 - The third part being the description of your initial impression before looking closer. Data in the text file... 01,WestHall,The room is filled with old pottery and cobwebs.: 011,Searching the room you notice a hole where a key might fit.: 02,EastHall,Several torches line the walls, and a chest rests upon a stone slab in the back.: 021,Searching the room you notice the flames on the torches are not moving, almost as if...: Is it possible to pull this data out in chunks using StreamReader? Unless... I just thought of something as I write this very moment. Can I say... ReadToEnd, this entire text file and say drop it in to a string variable. Then from their use some sort of character searching in the string? Thanks for your help ahead of time. :-)
-
I just read an article on parsing in c# and they said that unlike older languages, parsing in c# in simple terms, for a beginner, can be a nightmare. All I want to do is store room descriptors in a text file with a dillimeter and a value. - The first value is used to find the correct line to print. e.g 01 - The second part being the name of the room, or unless its the rooms look value, e.g 011 - The third part being the description of your initial impression before looking closer. Data in the text file... 01,WestHall,The room is filled with old pottery and cobwebs.: 011,Searching the room you notice a hole where a key might fit.: 02,EastHall,Several torches line the walls, and a chest rests upon a stone slab in the back.: 021,Searching the room you notice the flames on the torches are not moving, almost as if...: Is it possible to pull this data out in chunks using StreamReader? Unless... I just thought of something as I write this very moment. Can I say... ReadToEnd, this entire text file and say drop it in to a string variable. Then from their use some sort of character searching in the string? Thanks for your help ahead of time. :-)
jblouir wrote:
they said that unlike older languages, parsing in c# in simple terms, for a beginner, can be a nightmare.
Written by a moron then. The string class in C# has similar methods to CString in C++, it's at least as good as that class. It can be difficult to learn regex, but that's a moot point. You don't have to use regex, and if you do need it, it's wonderful that it's there.
jblouir wrote:
All I want to do is store room descriptors in a text file with a dillimeter and a value.
The string's split method will let you take a line and turn it into an array of strings, with each string being one of your values. string [] lines = File.ReadAllLines(@"c:\file.txt"); foreach (string line in lines) { string [] elements = line.split(new char [] { ',' } ); if (elements.Count > 0) { int n; if (int.TryParse(elements[0], out n)) { // the first element was a number, now stored in n } } if (elements.Count > 1) { // process elements[1], which is WestHall in the first instance. } // etc. }
Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )
-
I just read an article on parsing in c# and they said that unlike older languages, parsing in c# in simple terms, for a beginner, can be a nightmare. All I want to do is store room descriptors in a text file with a dillimeter and a value. - The first value is used to find the correct line to print. e.g 01 - The second part being the name of the room, or unless its the rooms look value, e.g 011 - The third part being the description of your initial impression before looking closer. Data in the text file... 01,WestHall,The room is filled with old pottery and cobwebs.: 011,Searching the room you notice a hole where a key might fit.: 02,EastHall,Several torches line the walls, and a chest rests upon a stone slab in the back.: 021,Searching the room you notice the flames on the torches are not moving, almost as if...: Is it possible to pull this data out in chunks using StreamReader? Unless... I just thought of something as I write this very moment. Can I say... ReadToEnd, this entire text file and say drop it in to a string variable. Then from their use some sort of character searching in the string? Thanks for your help ahead of time. :-)
jblouir wrote:
I just read an article on parsing in c# and they said that unlike older languages, parsing in c# in simple terms, for a beginner, can be a nightmare.
Anything for a beginner is a nightmare, until they learn it's really very easy! I have no idea where you read that, but stop.
jblouir wrote:
Is it possible to pull this data out in chunks using StreamReader?
Sure. But, you don't have to use ReadToEnd. Just read a line of the file (StreamReader.ReadLine), split the line on your delimitter (String.Split) to get the string values in an array, then process the first item in the array. That will tell you how many more items there should b in the array and what to do with them. Read the next line in the file. Rinse and Repeat.
A guide to posting questions on CodeProject[^]
Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
2006, 2007 -
jblouir wrote:
they said that unlike older languages, parsing in c# in simple terms, for a beginner, can be a nightmare.
Written by a moron then. The string class in C# has similar methods to CString in C++, it's at least as good as that class. It can be difficult to learn regex, but that's a moot point. You don't have to use regex, and if you do need it, it's wonderful that it's there.
jblouir wrote:
All I want to do is store room descriptors in a text file with a dillimeter and a value.
The string's split method will let you take a line and turn it into an array of strings, with each string being one of your values. string [] lines = File.ReadAllLines(@"c:\file.txt"); foreach (string line in lines) { string [] elements = line.split(new char [] { ',' } ); if (elements.Count > 0) { int n; if (int.TryParse(elements[0], out n)) { // the first element was a number, now stored in n } } if (elements.Count > 1) { // process elements[1], which is WestHall in the first instance. } // etc. }
Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )
Damn, you beat me to it! Frickin' router problems!
A guide to posting questions on CodeProject[^]
Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
2006, 2007 -
Damn, you beat me to it! Frickin' router problems!
A guide to posting questions on CodeProject[^]
Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
2006, 2007ROTFL - you're usually the one who beats me :P
Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )
-
I just read an article on parsing in c# and they said that unlike older languages, parsing in c# in simple terms, for a beginner, can be a nightmare. All I want to do is store room descriptors in a text file with a dillimeter and a value. - The first value is used to find the correct line to print. e.g 01 - The second part being the name of the room, or unless its the rooms look value, e.g 011 - The third part being the description of your initial impression before looking closer. Data in the text file... 01,WestHall,The room is filled with old pottery and cobwebs.: 011,Searching the room you notice a hole where a key might fit.: 02,EastHall,Several torches line the walls, and a chest rests upon a stone slab in the back.: 021,Searching the room you notice the flames on the torches are not moving, almost as if...: Is it possible to pull this data out in chunks using StreamReader? Unless... I just thought of something as I write this very moment. Can I say... ReadToEnd, this entire text file and say drop it in to a string variable. Then from their use some sort of character searching in the string? Thanks for your help ahead of time. :-)
The other responses are correct, but have you considered using XML?
-
I just read an article on parsing in c# and they said that unlike older languages, parsing in c# in simple terms, for a beginner, can be a nightmare. All I want to do is store room descriptors in a text file with a dillimeter and a value. - The first value is used to find the correct line to print. e.g 01 - The second part being the name of the room, or unless its the rooms look value, e.g 011 - The third part being the description of your initial impression before looking closer. Data in the text file... 01,WestHall,The room is filled with old pottery and cobwebs.: 011,Searching the room you notice a hole where a key might fit.: 02,EastHall,Several torches line the walls, and a chest rests upon a stone slab in the back.: 021,Searching the room you notice the flames on the torches are not moving, almost as if...: Is it possible to pull this data out in chunks using StreamReader? Unless... I just thought of something as I write this very moment. Can I say... ReadToEnd, this entire text file and say drop it in to a string variable. Then from their use some sort of character searching in the string? Thanks for your help ahead of time. :-)
Thanks for your help previously, been implementing it into my code. Its changed a bit because I had problems with getting the .Count to work, doesnt appear to be in my namespaces. but anyway heres what I have, the program doesnt get any syntax errors, but during the program running I get an error that...(scroll down) #region dataload() // Extracting the data from databin and dumping it in to an array. static void dataload() { int intIndex = 0; string strRoom = "anything"; string strDesc = "anything"; string strLook = "anything"; int intElement = 0; int i = 0; string[] lines = File.ReadAllLines("databin.txt"); foreach (string line in lines) { string[] elements = line.Split(new char[] { ',' }); intElement = elements.Length; if (intElement > 0) { int n; if (int.TryParse(elements[0], out n)) { // the first element was a number, now stored in n intIndex = n; } } if (intElement > 1) { strRoom = elements[1]; } if (intElement > 2) { strDesc = elements[2]; } if (intElement > 3) { strLook = elements[3]; } RoomData descriptor = new RoomData(intIndex, strRoom, strDesc, strLook); GlobalVars.ArrRoomData.SetValue(descriptor,i); i = i++; // etc. } } #endregion the error points to GlobalVars.ArrRoomData.SetValue(descriptor,i); saying that the object reference not set to an instance of an object ArrRoomData is declared in a class called GlobalVars (Global Variables), where... private static object[] arrRoomData; public object[] ArrRoomData { get { return arrRoomData; } set { arrRoomData = value; } } I have a feeling that once it gets past this error im going to get another one on the fact that its going to try and declare another object named descriptor, which im guessing I can I can do an if check on a boolean to see if its already been declared once.
-
The other responses are correct, but have you considered using XML?
I am trying to start with basic stuff, instead of jumping into 2d/3d graphics and using databases, so im just making a text adventure (In console) with simple data files for storing data, I might use serialisation for saving the game state(save games). After that then ill move up to the fancier code. :o)
-
Thanks for your help previously, been implementing it into my code. Its changed a bit because I had problems with getting the .Count to work, doesnt appear to be in my namespaces. but anyway heres what I have, the program doesnt get any syntax errors, but during the program running I get an error that...(scroll down) #region dataload() // Extracting the data from databin and dumping it in to an array. static void dataload() { int intIndex = 0; string strRoom = "anything"; string strDesc = "anything"; string strLook = "anything"; int intElement = 0; int i = 0; string[] lines = File.ReadAllLines("databin.txt"); foreach (string line in lines) { string[] elements = line.Split(new char[] { ',' }); intElement = elements.Length; if (intElement > 0) { int n; if (int.TryParse(elements[0], out n)) { // the first element was a number, now stored in n intIndex = n; } } if (intElement > 1) { strRoom = elements[1]; } if (intElement > 2) { strDesc = elements[2]; } if (intElement > 3) { strLook = elements[3]; } RoomData descriptor = new RoomData(intIndex, strRoom, strDesc, strLook); GlobalVars.ArrRoomData.SetValue(descriptor,i); i = i++; // etc. } } #endregion the error points to GlobalVars.ArrRoomData.SetValue(descriptor,i); saying that the object reference not set to an instance of an object ArrRoomData is declared in a class called GlobalVars (Global Variables), where... private static object[] arrRoomData; public object[] ArrRoomData { get { return arrRoomData; } set { arrRoomData = value; } } I have a feeling that once it gets past this error im going to get another one on the fact that its going to try and declare another object named descriptor, which im guessing I can I can do an if check on a boolean to see if its already been declared once.
It's perhaps intElement.Length
jblouir wrote:
string strRoom = "anything"; string strDesc = "anything"; string strLook = "anything";
This is outside your loop, so if a row fails, instead of a default, it will continue to contain the last value found.
jblouir wrote:
saying that the object reference not set to an instance of an object
Odds are that a line doesn't have enough elements, and removing Count is making a check that is just invalid.
Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )
-
I am trying to start with basic stuff, instead of jumping into 2d/3d graphics and using databases, so im just making a text adventure (In console) with simple data files for storing data, I might use serialisation for saving the game state(save games). After that then ill move up to the fancier code. :o)
-
It's perhaps intElement.Length
jblouir wrote:
string strRoom = "anything"; string strDesc = "anything"; string strLook = "anything";
This is outside your loop, so if a row fails, instead of a default, it will continue to contain the last value found.
jblouir wrote:
saying that the object reference not set to an instance of an object
Odds are that a line doesn't have enough elements, and removing Count is making a check that is just invalid.
Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )
At the bottom of the code where the particular code of line is highlighted. descriptor is holding the correct values for index, room name, room desc, and room look. The .Count I got around by using .Length which returns the number of elements in that line which logically to me does the same thing.