Click here to Skip to main content
15,886,077 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.


C#
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();
}
Posted
Updated 30-Mar-10 1:39am
v2

There are a few things wrong with this: lets start with a biggie!
C#
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:
C#
while ((ThanhPho = sr.ReadLine()) != null);
   {
   ...
   }
sr.Close();

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)
   {
   ...
   }

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")
or even
ThanhPho.StartsWith("Tp:")


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!
 
Share this answer
 
Cannot you just add the ThanhPho.Substring(3)?
:)
 
Share this answer
 
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...
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900