[英]Reading line by line
我有一個生成純文本文件的程序。 結構(布局)始終相同。 例:
LinkLabel
"Hello, this text will appear in a LinkLabel once it has been
added to the form. This text may not always cover more than one line. But will always be surrounded by quotation marks."
240, 780
因此,要解釋該文件中發生了什么:
Control
Text
Location
有人做過嗎? 有人能提供有關我將如何執行此操作的任何幫助,建議或意見嗎?
我查找了CSV文件,但是對於看起來如此簡單的東西來說似乎太復雜了。
謝謝傑瑟
我將嘗試寫下算法,以解決這些問題的方式(在注釋中):
// while not at end of file
// read control
// read line of text
// while last char in line is not "
// read line of text
// read location
嘗試編寫符合每個注釋要求的代碼,您應該能夠弄清楚。
HTH。
您可以使用正則表達式從文本中獲取行:
MatchCollection lines = Regex.Matches(File.ReadAllText(fileName), @"(.+?)\r\n""([^""]+)""\r\n(\d+), (\d+)\r\n");
foreach (Match match in lines) {
string control = match.Groups[1].Value;
string text = match.Groups[2].Value;
int x = Int32.Parse(match.Groups[3].Value);
int y = Int32.Parse(match.Groups[4].Value);
Console.WriteLine("{0}, \"{1}\", {2}, {3}", control, text, x, y);
}
您正在嘗試實現一個解析器,而為此的最佳策略是將問題分成更小的部分。 並且您需要一個TextReader
類,使您能夠讀取行。
您應該將ReadControl
方法分為三個方法: ReadControlType
, ReadText
和ReadLocation
。 每個方法僅負責讀取應讀取的項目,並將TextReader
留在下一個方法可以使用的位置。 這樣的事情。
public Control ReadControl(TextReader reader)
{
string controlType = ReadControlType(reader);
string text = ReadText(reader);
Point location = ReadLocation(reader);
... return the control ...
}
當然,ReadText是最有趣的一種,因為它跨越多行。 實際上,這是一個調用TextReader.ReadLine
的循環,直到該行以引號結束為止:
private string ReadText(TextReader reader)
{
string text;
string line = reader.ReadLine();
text = line.Substring(1); // Strip first quotation mark.
while (!text.EndsWith("\"")) {
line = reader.ReadLine();
text += line;
}
return text.Substring(0, text.Length - 1); // Strip last quotation mark.
}
這種東西很煩人,從概念上講很簡單,但最終可能會產生粗糙的代碼。 您有一個相對簡單的案例:每個文件一個記錄,如果有很多記錄,則變得更加困難,並且您希望很好地處理格式錯誤的記錄(考慮為C#這樣的語言編寫解析器。
對於大規模問題,可以使用如下語法驅動的解析器: link text
您的大部分復雜性來自文件缺乏規則性。 第一個字段以nwline終止,第二個字段以引號分隔,第三個字段以逗號終止...
我的第一個建議是調整文件的格式,以使其真正易於解析。 您編寫文件即可控制自己。 例如,僅在文本中沒有換行,並且每個項目都在自己的行上。 然后,您只需閱讀四行,就可以完成工作。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.