作者qweqweqweqwe (啪)
看板C_Sharp
標題Re: [問題] 請問要如何從.txt內讀取字元
時間Wed Oct 10 17:51:17 2007
※ 引述《DolphinEdwin (.)》之銘言:
: 我的問題是.txt內的檔案假設為:
: 1 5 1 2
: 2 4 6 7
: 3 6 4 5
: 4 7 2 1
: 5 3 10 9
: 6 1 11 1
: 7 8 9 2
: 8 9 1 12
: 9 1 7 5
: 10 2 12 13
: 11 1 5 6
: 數字間都隔一個空格
: 假設我宣告了陣列 a[] b[] c[] d[]
: 想要讓他讀取並儲存成為
: a[0]=1 a[1]=2 a[2]=3 a[3]=4 ...a[] 為.txt第一行
: b[0]=5 b[1]=4 b[2]=6 b[3]=7 ...b[] 為.txt第二行
: c[0]=1 c[1]=6 c[2]=4 c[3]=2 ...c[] 為.txt第三行
: d[0]=2 d[1]=7 d[2]=5 d[3]=1 ...d[] 為.txt第四行
: 請問該怎麼做呢??
: 或者是全部由左而右由上而下儲存成為
: a[0]=1 a[1]=5 a[2]=1 a[3]=2
: a[4]=2 a[5]=4 a[6]=6 a[7]=7.....
: 目前我只會一次只讀取1個字元的
: StreamReader sr = new StreamReader("d:\\xxx.txt");
: char[] a = new char[sr.BaseStream.Length];
: sr.Read(a, 0, a.Length);
: sr.Close();
: 不知道怎麼一次讀取多個字元...
: 請各位高手幫忙了拜託拜託...
: 感激不盡
using (StreamReader str = new
StreamReader("C:\\xxx.txt",Encoding.Default))
{
String line;
line = str.ReadLine();
temp = line.Trim().Split(new char[] { ' ' });
str.Close();
}
這樣..?
temp[0]=1 temp[1]=5 temp[2]=1 temp[3]=2
試試看
我沒compile過QQ
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 125.225.124.59
→ qweqweqweqwe:忘了加 String[] temp;在最前面 10/10 18:05
推 DolphinEdwin:感謝 10/12 23:21