2015-09-27 74 views
0

我有一个文件,我需要保存为一个数组。我正在尝试使用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]; 
} 

回答

2

你应该只能够使用类似的代码,你有什么上面:

OriginalArray[i] = Convert.ToInt32(sr.ReadLine()); 

每次sr.ReadLine把它叫做1线递增数据指针,因此通过数组迭代在文本文件中。

+0

OMG刚刚看到你的答案了!非常感谢你!你的回答非常简单,让我意识到我是多么渴望大声笑! –

0

试试这个

OpenFileDialog fd = new OpenFileDialog(); 

if (fd.ShowDialog() == DialogResult.OK) 
{ 
    StreamReader reader = new StreamReader(fd.OpenFile()); 

    var list = new List<int>(); 

    while (!reader.EndOfStream) 
    { 
     var line = reader.ReadLine(); 
     int value = 0; 
     if (!string.IsNullOrWhiteSpace(line) && int.TryParse(line, out value)) 
      list.Add(value); 
    } 

    MessageBox.Show(list.Aggregate("", (x, y) => (string.IsNullOrWhiteSpace(x) ? "" : x + ", ") + y.ToString())); 
} 
0

您可以将整个文件读入一个字符串数组,然后解析(检查每一个的完整性)。

喜欢的东西:

int[] Original; 
//Load File 
private void mnuLoad_Click_1(object sender, EventArgs e) 
{ 
    //code to load the numbers from a file 
    var fd = new OpenFileDialog(); 

    //open the file dialog and check if a file was selected 
    if (fd.ShowDialog() == DialogResult.OK) 
    { 
     var file = fd.FileName; 

     try 
     { 
      var ints = new List<int>(); 
      var data = File.ReadAllLines(file); 

      foreach (var datum in data) 
      { 
       int value; 
       if (Int32.TryParse(datum, out value)) 
       { 
        ints.Add(value); 
       } 
      } 

      Original = ints.ToArray(); 

     } 
     catch (IOException) 
     { 
      // blah, error 
     } 
    } 
} 
0

另一种方式来做到这一点,如果你想阅读到文件的末尾,你不知道它有多长while循环:

String line = String.Empty; 
int i=0; 
while((line = sr.ReadLine()) != null) 
{ 
    yourArray[i] = Convert.ToInt32(line); 
    i++; 

    //but if you only want to write to the file w/o any other operation 
    //you could just write w/o conversion or storing into an array 
    sw.WriteLine(line); 
    //or sw.Write(line + " "); if you'd like to have it in one row 
} 
0
//using linq & anonymous methods (via lambda) 
string[] records = File.ReadAllLines(file); 
int[] unsorted = Array.ConvertAll<string, int>(records, new Converter<string, int>(i => int.Parse(i))); 
相关问题