2015-09-27 71 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))); 
相關問題