将数据添加到数组 [英] add data to array

查看:82
本文介绍了将数据添加到数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在将数据添加到数组之后,我有一个从文件txt读取的代码.但是我不能,代码行不通.我文件中的数据:

I have a code to read from file txt, after that add data to array. But I can''t, code not work. Data in my file:

Tp:Newyork
Tp:Cali
Tp:LA
...



请帮助我解决此错误.




please help me fix this error.


void ReadCity()
{
  string ThanhPho;
  FileStream fs = new FileStream("D:\\City.txt", FileMode.Open, FileAccess.Read);
  StreamReader sr = new StreamReader(fs);
  sr.BaseStream.Seek(0, SeekOrigin.Begin);
  ThanhPho = sr.ReadLine();
  do
  {
    if (ThanhPho.Substring(0, 1) == "T")
    {
      int vitri = ThanhPho.IndexOf(":");
      Console.WriteLine(vitri);
      //City[i] = ThanhPho.Substring(vitri + 1);
      //i++;
    }
    else Console.WriteLine("Khong co");
    //City[i] = new string(ThanhPho);
    //Console.WriteLine(ThanhPho);
    //ThanhPho = sr.ReadLine();

    //City[i] = ThanhPho;
    //Console.Write(City[i]);
  } while (ThanhPho != null);
  ThanhPho = sr.ReadLine();
  sr.Close();
  fs.Close();
}

推荐答案

这有一些问题:让我们从大个子开始吧!
There are a few things wrong with this: lets start with a biggie!
ThanhPho = sr.ReadLine();
do
   {
   ...
   } while (ThanhPho != null);
ThanhPho = sr.ReadLine();
sr.Close();

从文件中读取一行;处理它,如果存在,则再次处理它.然后再次.然后再次.如果它不存在,它将从已经必须为空的文件中读取另一行,然后关闭该文件并退出例程.
您实际上要说的是:

This reads a line from a file; processes it, and if it exists, goes around to process it again. And again. And again. If it doesn''t exist, it read another line (from the file that already must be empty) and then closes the file and exits the routine.
What you actually meant to say was:

while ((ThanhPho = sr.ReadLine()) != null);
   {
   ...
   }
sr.Close();


这将处理更多的行,并可能至少解决您的问题之一.

为什么要打开一个流,然后将其传递给另一个流?
打开它,阅读,关闭,处置.对您的应用程序而言,最简单的方法是使用File.ReadAllLines:


Which will process more lines, and may fix at least one of your problems.

Why are you opening a stream, then passing it to another stream?
Open it, read it, close it, dispose it. The easiest way to do that for your application is just to use File.ReadAllLines:

string[] lines = File.ReadAllLines(D:\\City.txt");
foreach (string ThanhPho in lines)
   {
   ...
   }


如果必须使用流,则将它们封装在using块中,以确保将其关闭并正确处理.

这不是一个错误,但将使它更易于阅读:替换您的


If you must use streams, then enclose them in using blocks, to ensure they are closed and disposed properly.

This isn''t an error, but it will make it easier to read: replace your

ThanhPho.Substring(0, 1) == "T"

with

ThanhPho.StartsWith("T")

甚至

ThanhPho.StartsWith("Tp:")



这不是一个错误,但这是一个很好的主意:始终在任何if ... else ...语句周围使用大括号-这样一来,如果以后再添加一条语句,它们已经存在并且您不知道.''最终会导致您其他人可能产生的困惑.即



This isn;t quite an error, but it is a very good idea: Always use curly brackets around any if...else... statement - that way if you add a statement later, they are already there and you don''t end up with the confusion your else could have. I.e.

if(ThanhPho.StartsWith("T"))
   {
   ...
   }
else
   {
   Console.WriteLine("Khong co");
   }



在不了解城市"定义的情况下,很难进一步提供帮助!



Without knowing more about the definition of "City" it is difficult to help further!


您不能只添加ThanhPho.Substring(3)吗?
:)
Cannot you just add the ThanhPho.Substring(3)?
:)


感谢您的回答.我是修复错误,下面是我的代码.但是当我很快收到关于我的问题的答复时,我感到非常高兴.您的回答对我来说是个好主意.再次感谢!

班级计划
{
string [] City = new string [100];
int i;
}

专用的无效readword(字符串wordfile)
{
字符串ThanhPho;
StreamReader sread = new StreamReader(wordfile);
sread.BaseStream.Seek(0; seekOrigin.Begin);
ThanhPho = sreead.ReadLine();
while(ThanhPho!= null)
{int vitri = -1;
City [i] = ThanhPho.Substring(vitri + 1);
i ++;
ThanhPho = sread.ReadLine();
}
}

我通过电话输入密码;可能有一些大写或小写错误...
thanks for your answer. I was fix error, below is my code. But i''m very happy when receive reply about my question very fast. Your answer is good idea for me. Thank again!

Class program
{
string[] City=new string[100];
int i;
}

Private void readword(string wordfile)
{
string ThanhPho;
StreamReader sread = new StreamReader(wordfile);
sread.BaseStream.Seek(0;seekOrigin.Begin);
ThanhPho=sreead.ReadLine();
while(ThanhPho!=null)
{ int vitri=-1;
City[i]=ThanhPho.Substring(vitri+1);
i++;
ThanhPho=sread.ReadLine();
}
}

i type code by phone; may be have some error upcase or lowercase...


这篇关于将数据添加到数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆