에 대한 문자열 목록 FO를 반환하는 함수를 만드는 방법 인쇄를 완료 한 후 하나의 그룹, 그 다음에 값을 보유 할 클래스?
public static List<string> ReadGroup(TextReader tr)
{
string line = tr.ReadLine();
List<string> lines = new List<string>();
while (line != null && line.Length > 0)
{
lines.Add(line);
}
// change this to line == null if you have groups with no lines
if (lines.Count == 0)
{
return null;
}
return lines;
}
그럼 당신은 목록에 인덱스 라인에 액세스 할 수 있습니다
String line;
String path = "c:/sample.txt";
using (StreamReader sr = new StreamReader(path))
{
while ((List<string> lines = ReadGroup(sr)) != null)
{
// you might want to check for lines.Count >= 4 if you will
// have groups with fewer lines to provide a better error
//display the readed lines in the text box
disTextBox.AppendText(string.Format("{0}\t{1}\t{2}{3}",
lines[0], lines[2], lines[3], Environment.NewLine);
}
sr.Close();
}
난 당신의 첫 번째는 "포 보 1255"에 별도의 라인을 가지고 있음을 알 수 있습니다. 의미있는 파일을 만드는 데 필요한 형식이 무엇인지 알아야합니다. 그룹의 마지막 두 줄이 도시와 국가 인 경우 줄 수를 사용해야합니다.
class LineGroup // name whatever the data contains
{
public string Name { get; set; }
public string Code { get; set; }
public string City { get; set; }
public string Country { get; set; }
public LineGroup(List<string> lines)
{
if (lines == null || lines.Count < 4)
{
throw new ApplicationException("LineGroup file format error: Each group must have at least 4 lines");
}
Name = lines[0];
Code = lines[1];
City = lines[lines.Count - 2];
Country = lines[lines.Count - 1];
}
}
그리고 프로세스 :
while ((List<string> lines = ReadGroup(sr) != null)
{
LineGroup group = new LineGroup(lines);
//display the readed lines in the text box
disTextBox.AppendText(string.Format("{0}\t{1}\t{2}{3}",
group.Name, group.City, group.Country, Environment.NewLine);
}
이 예입니다. 나는 텍스트 파일의 큰 라인. plese 내 문제를 해결하는 데 도움이됩니다. – Chamal
숙제? 파일의 모든 레코드가 동일한 형식으로 보장됩니까? – arootbeer
아니요 다를 수 있습니다. – Chamal