这就是我糟糕的代码
class begin
{
public static string[] Reader()
{
string[] theMap = System.IO.File.ReadAllLines(@"C:\Users\Public\Console Slayer\Map\map.txt");
string[] Map = theMap.Clone() as string[];
return Map;
}
public static void Printer()
{
foreach (string line in Reader())
{
Console.WriteLine(line);
}
}
static void Main()
{
Reader();
Printer();
}
}
我想将Map字符串转换为2D数组以供功能使用。 我是编程的新手,我知道我的代码很糟糕。
答案 0 :(得分:1)
尝试使用
Microsoft.VisualBasic.FileIO.TextFieldParser
是的,它是一个VB组件,但它可以工作。无需重新发明轮子。样品用法:
OpenFileDialog od = new OpenFileDialog();
od.Filter = "Tab delimited file (*.txt)|*.txt";
if (od.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
using (var reader = new Microsoft.VisualBasic.FileIO.TextFieldParser(od.FileName))
{
reader.TextFieldType = Microsoft.VisualBasic.FileIO.FieldType.Delimited;
reader.Delimiters = new string[] { "\t" }; // the delimeter of the lines in your file
reader.ReadLine(); // skip header if needed, ignore titles
while (!reader.EndOfData)
{
try
{
var currentRow = reader.ReadFields(); // string array
// Include code here to handle the row.
}
catch (Microsoft.VisualBasic.FileIO.MalformedLineException vex)
{
MessageBox.Show("Line " + vex.Message + " is invalid. Skipping");
}
}
}
}