关于C#的问题

shancao 2009-03-18 10:53:29
各位大侠好:
我有个问题不知道如何解决。有一个文件内容是:
1,2,3,4
11,2,32,31
3,4,5,7
54,21,22,11
如何读取,并且存储在一个整型(int)二维数组中a【4】【4】。也就是把这个内容解析出来。需要代码,谢谢了。
...全文
54 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
贫僧又回来了 2009-03-18
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 sleep0110 的回复:]
StreamReader sr = new StreamReader(@"C:\123.txt");//可以指定下编码,呵呵,如果固定就是你给的那样的文件,那也没什么关系[/Quote]
睡神在睡觉 2009-03-18
  • 打赏
  • 举报
回复
using System.IO;


int[,] int_array = new int[4, 4];
StreamReader sr = new StreamReader(@"C:\123.txt");//txt文件存在路径
string line = sr.ReadLine();
int j = 0;
while (line != null)
{
string[] str = line.Split(new char[] { ',' });
for (byte i = 0; (i < 4) && (i < str.Length); i++)
{
int_array[j, i] = int.Parse(str[i].Trim());
}
j++;
line = sr.ReadLine();
}
//经过测试没问题
//文本格式:
//1,2,3,4
//11,2,32,31
//3,4,5,7
//54,21,22,11


不知道你文件是什么文件,如果是文本的话,这样就可以了
kbtjh 2009-03-18
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 sleep0110 的回复:]
C# codeusing System.IO;


int[,] int_array = new int[4, 4];
StreamReader sr = new StreamReader(@"C:\123.txt");//txt文件存在路径
string line = sr.ReadLine();
int j = 0;
while (line != null)
{
string[] str = line.Split(new char[] { ',' });
for (byte i = 0; (i < 4) && (i < str.Length); i++)
{
int_array[j, i] = int.Parse(str[i].Trim());
}
j++;
line = sr.ReadLine();
}…
[/Quote]

110,566

社区成员

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

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

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