如何从txt中读取,并存入数组中

nana7523016 2011-03-27 09:00:22
a.txt中的内容格式为:

0,0,0.1//表示第0行第0列是0.1
0,1,0.2
1,0,0.3
1,1,0.4

怎样从txt中读取内容并存入数组int[2,2] A 中?
最好有整个代码,谢了~~小女感激不尽
...全文
224 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
Katherine 2011-03-28
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 zilong4460072 的回复:]

0,0,1
0,1,2
1,0,3
1,1,4
存放在G盘的ayyar.txt中
C# code

class Program
{
static void Main(string[] args)
{
GetNumber(@"G:\array.txt");
Console.Read();
……
[/Quote]
+1
钱币老顽童 2011-03-28
  • 打赏
  • 举报
回复
private static void GetNumber(string filePath)
{
int row = 2;
int column = 2;
int[,] intArray = new int[row, column];
try
{
using (StreamReader sr = new StreamReader(filePath))
{
while (!sr.EndOfStream)
{
string str = sr.ReadLine();
string[] strArray = str.Split(',');
intArray[int.Parse(strArray[0]), int.Parse(strArray[1])] = int.Parse(strArray[2]);
}
}
}
catch(Exception e)
{
string msg = e.Message;
}
for (int i = 0; i < column; i++)
{
for (int j = 0; j < row; j++)
{
Console.Write(intArray[i,j].ToString() + " ");
}
Console.Write("\n");
}
}
Joop_Song 2011-03-28
  • 打赏
  • 举报
回复
请参照IO
sfxdawn 2011-03-28
  • 打赏
  • 举报
回复
六樓的方法不錯。
zilong4460072 2011-03-28
  • 打赏
  • 举报
回复
0,0,1
0,1,2
1,0,3
1,1,4
存放在G盘的ayyar.txt中

class Program
{
static void Main(string[] args)
{
GetNumber(@"G:\array.txt");
Console.Read();
}

private static void GetNumber(string filePath)
{
int row = 2;
int column = 2;
int[,] intArray = new int[row, column];
try
{
using (StreamReader sr = new StreamReader(filePath))
{
while (!sr.EndOfStream)
{
string str = sr.ReadLine();
string[] strArray = str.Split(',');
intArray[int.Parse(strArray[0]), int.Parse(strArray[1])] = int.Parse(strArray[2]);
}
}
}
catch(Exception e)
{
string msg = e.Message;
}
for (int i = 0; i < column; i++)
{
for (int j = 0; j < row; j++)
{
Console.Write(intArray[i,j].ToString() + " ");
}
Console.Write("\n");
}
}
}
nana7523016 2011-03-28
  • 打赏
  • 举报
回复
非常感谢了 热心人真多呵呵[Quote=引用 6 楼 zilong4460072 的回复:]
0,0,1
0,1,2
1,0,3
1,1,4
存放在G盘的ayyar.txt中

C# code

class Program
{
static void Main(string[] args)
{
GetNumber(@"G:\array.txt");
Console.Re……
[/Quote]
guoyanhong1111 2011-03-27
  • 打赏
  • 举报
回复
using (StreamReader sr2 = new StreamReader(filePath, System.Text.Encoding.GetEncoding("gb2312"), false))
{
while (sr2.ReadLine()!= null)
{
//放入数组
}
}
子夜__ 2011-03-27
  • 打赏
  • 举报
回复
大山里的松 2011-03-27
  • 打赏
  • 举报
回复
StreamReader 一行一行的读

split 到 数组中
然后分析 填充到 int[2,2] A 中
bourbon1795 2011-03-27
  • 打赏
  • 举报
回复
readline方法读出一行,然后自己处理

110,539

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧