将数据从文件写入数组

将数据从文件写入数组

本文介绍了C#将数据从文件写入数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好!
我是C#编程的新手,我想我的问题对您来说很简单:-D
您能帮我解决这个问题吗?

我有一个带有此类数据的文本文件:

拉里#123542
安东#234568
Suzie#234528
............


我只需要选择#char之后的数据到整数数组中即可.
所以我需要这样的东西:

a [1] = 123542;
a [2] = 234568;
a [3] = 234528;
......


我正在Visual Studio''10中工作.
谢谢大家的回答! ;)
俄罗斯的良好祝愿!

PS:对不起,如果我的英语有点错误:confused:

Hi everybody!
I''m newbie in C# programming and i think my question is rather simple for you :-D
Can you help me with such problem:

I have a textfile with such data:

Larry#123542
Anton#234568
Suzie#234528
............
etc.

I just need to pick out the data after # char into the array of integer.
So i need to have something like this:

a[1]=123542;
a[2]=234568;
a[3]=234528;
...........
etc.

I''m working in Visual Studio ''10.
Thanks everybody for your answers! ;)
Best wishes from Russia!

P.S.: Sorry for my English if it is a little wrong :confused:

推荐答案

List<string> data = new List<string>();

foreach(string line in file)
{
  data.add( line.Split(''#'')[1] );
}


const string f = "TextFile1.txt";
// 1
// Declare new List.
List<string> lines = new List<string>();
// 2
// Use using StreamReader for disposing.
using (StreamReader r = new StreamReader(f))
{
  // 3
  // Use while != null pattern for loop
  string line;
  while ((line = r.ReadLine()) != null)
  {
      // 4
      // Insert logic here.
      // ...
      // "line" is a line in the file. Add it to our List.
                lines.Add(line);
  }
}
// Now get the array from the list and use it
// lines.ToArray(); 


这篇关于C#将数据从文件写入数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 21:58