所以我正在做一个小历史测试来帮助我学习。目前我对数组进行了硬编码,这就是我想从文本文件中读取数组的方式。我想更改此设置,以便我可以通过更改文本文件来添加和删除日期和事件
static string[,] dates = new string[4, 2]
{
{"1870", "France was defeated in the Franco Prussian War"},
{"1871", "The German Empire Merge into one"},
{"1905", "The \"Schliffin PLan\" devised"},
{"1914", "The Assassination of Franz Ferdinand and the start of WW1"},
//etc
}
数组只是一个占位符,用于从文本文件中读取内容。我知道我应该使用StreamReader然后拆分它,但我不知道该怎么做。我尝试过使用2个列表然后按照这个
将它们推送到数组中//for date/event alteration
isDate = true;
//for find the length of the file, i don't know a better way of doing this
string[] lineAmount = File.ReadAllLines("test.txt");
using (StreamReader reader = new StreamReader("test.txt"))
{
for (int i = 0; i <= lineAmount.Length; i++)
{
if (isDate)
{
//use split here somehow?
dates.Add(reader.ReadLine());
isDate = false;
}
else
{
events.Add(reader.ReadLine());
isDate = true;
}
}
}
string[] dates2 = dates.ToArray();
string[] events2 = events.ToArray();
string[,] info = new string[,] { };
//could use dates or events for middle (they have the same amount)
//push the lists into a 2d array
for (int i = 0; i <= events2.Length; i++)
{
//gives an index out of bounds of array error
//possibly due to the empty array declaration above? not sure how to fix
info[0, i] = dates2[i];
info[1, i] = events2[i];
}
这是如何设置txt文件的示例:
1870年,法国 - 普鲁士战争(法国击败),
1871年,德意志帝国合并,
所以你可以说,文本文件几乎与数组相同。所以我的问题是,如何将此文本文件读入此格式的二维数组
答案 0 :(得分:1)
这里最大的问题是你正在尝试用数组做这件事。 除非你的程序在开始时知道有多少行,否则它不知道制作数组有多大。你要么必须猜测(最糟糕的是容易出错而且效率最低)或者扫描文件中有多少换行符(效率也很低)。
只需使用列表并将您已读过的每一行添加到列表中。
如果每个条目的第二部分中没有逗号,则以下内容会解析您提及的文件:
List<string[ ]> entries = new List<string[ ]>( );
using ( TextReader rdr = File.OpenText( "TextFile1.txt" ) )
{
string line;
while ( ( line = rdr.ReadLine( ) ) != null )
{
string[ ] entry = line.Split( ',' );
entries.Add( entry );
}
}
获得清单后,随心所欲。列表成员可以像数组一样访问。主要区别在于列表是一个动态大小的对象,而一个数组卡在你最初制作它的大小。
列表将是文本文件的精确副本,减去逗号,每个字符串数组的第一个元素中的日期和第二个元素中的文本。
这会将原始文件输出回屏幕,逗号和所有内容:
foreach ( string[ ] entry in entries )
{
Console.WriteLine( string.Join( ",", entry ) );
}
如果你想从数组中获取一个随机元素(你说这是一个学习程序),那么你可以这样做:
Random rand = new Random();
while(true)
{
int itemIndex = rand.Next(0, entries.Length);
Console.WriteLine( "What year did {0} happen?", entries[itemIndex][1]);
string answer = Console.ReadLine();
if(answer == "exit")
break;
if(answer == entries[itemIndex][0])
Console.WriteLine("You got it!");
else
Console.WriteLine("You should study more...");
}
答案 1 :(得分:0)
这应该为你做。从文件中读取所有行,然后在逗号上拆分并将其存储在数组中。
//Read the entire file into a string array, with each element being one line
//Note that the variable 'file' is of type string[]
var file = File.ReadAllLines(@"C:\somePath.yourFile.txt");
var events = (from line in file //For every line in the string[] above
where !String.IsNullOrWhiteSpace(line) //only consider the items that are not completely blank
let pieces = line.Split(',') //Split each item and store the result into a string[] called pieces
select new[] { pieces[0], pieces[1].Trim() }).ToList(); //Output the result as a List<string[]>, with the second element trimmed of extra whitespace
如果您需要访问第一条记录,可以这样做:
var firstYear = events[0][0];
var firstDescription = events[0][1];
打破它......
ReadAllLines
只需打开一个文件,将内容读入数组,然后将其关闭。
LINQ声明: