我有一个文件,我需要保存为一个数组。我正在尝试使用StreamReader
将文本文件转换为整数数组。我只是不确定要在代码的末尾放入什么for循环。如何将.txt文件中的数字读入整数数组?
这是我到目前为止有:
//Global Variables
int[] Original;
//Load File
private void mnuLoad_Click_1(object sender, EventArgs e)
{
//code to load the numbers from a file
OpenFileDialog fd = new OpenFileDialog();
//open the file dialog and check if a file was selected
if (fd.ShowDialog() == DialogResult.OK)
{
//open file to read
StreamReader sr = new StreamReader(fd.OpenFile());
int Records = int.Parse(sr.ReadLine());
//Assign Array Sizes
Original = new int[Records];
int[] OriginalArray;
for (int i = 0; i < Records; i++)
{
//add code here
}
}
.txt文件是:
5
6
7
9
10
2
PS我是初学者,所以我的编码技能是非常基本的!
更新:我有使用Line.Split
然后将文件映射到数组的先前经验,但显然这不适用于此,所以我现在要做什么?
//as continued for above code
for (int i = 0; i < Records; i++)
{
int Line = int.Parse(sr.ReadLine());
OriginalArray = int.Parse(Line.Split(';')); //get error here
Original[i] = OriginalArray[0];
}
OMG刚刚看到你的答案了!非常感谢你!你的回答非常简单,让我意识到我是多么渴望大声笑! –