打开文本文件,通过内容和检查对环 [英] Open text file, loop through contents and check against
问题描述
所以我有一个通用的一些检查,我想实现:
公共静态布尔isNumberValid(串号)
{
}
和我想读一个文本文件的内容(仅包含数字),并检查每一行的数量和验证它是否使用 isNumberValid
的有效数字。然后我想将结果输出到一个新的文本文件,我得到了这个远:
私人无效button2_Click(对象发件人,EventArgs的)
{
INT大小= -1;
DialogResult的结果= openFileDialog1.ShowDialog(); //显示对话框。
如果(结果== DialogResult.OK)//测试结果。
{
字符串文件= openFileDialog1.FileName;
试
{
字符串文本= File.ReadAllText(文件);
尺寸= text.Length;使用(StringReader读卡器=新StringReader(文本))
{
的foreach(文字INT数)
{
//核对isNumberValid $
b $ b //结果写入到一个新的文本文件
}
}
}
赶上(IOException异常)
{
$} b $ b}
}
卡住从这里种类如果有人能帮助?
该文本文件包含在列表中的几个数字:
4564
4565
4455
块引用>
。等等
新的文本文件,我想写也只是用真或假追加到结尾的数字:
4564真
块引用>
解决方案您可以尝试这保持与您最初遵循的模式...
私人无效的button1_Click(对象发件人,EventArgs五)
{
DialogResult的结果= openFileDialog1.ShowDialog(); //显示对话框。
如果(结果== DialogResult.OK)//测试结果。
{
字符串文件= openFileDialog1.FileName;
试
{
使用(VAR读者=新的StreamReader(文件))
{
使用(VAR作家=新的StreamWriter(RESULTS.TXT))
{
串currentNumber;
,而((currentNumber = reader.ReadLine())!= NULL)
{
如果(IsNumberValid(currentNumber))
writer.WriteLine(的String.Format({0 }真,currentNumber));
}
}
}
}
赶上(IOException异常)
{
}
}
}
公共BOOL IsNumberValid(串号)
{
//无论代码,你用它来检查你的电话号码
}
So I have a generic number check that I am trying to implement:
public static bool isNumberValid(string Number) { }
And I want to read the contents of a textfile (only contains numbers) and check each line for the number and verify it is the valid number using
isNumberValid
. Then I want to output the results to a new textfile, I got this far:private void button2_Click(object sender, EventArgs e) { int size = -1; DialogResult result = openFileDialog1.ShowDialog(); // Show the dialog. if (result == DialogResult.OK) // Test result. { string file = openFileDialog1.FileName; try { string text = File.ReadAllText(file); size = text.Length; using (StringReader reader = new StringReader(text)) { foreach (int number in text) { // check against isNumberValid // write the results to a new textfile } } } catch (IOException) { } } }
Kind of stuck from here if anyone can help?
The textfile contains several numbers in a list:
4564
4565
4455
etc.
The new textfile I want to write would just be the numbers with true or false appended to the end:
4564 true
解决方案You could try this to keep with the pattern you were initially following...
private void button1_Click(object sender, EventArgs e) { DialogResult result = openFileDialog1.ShowDialog(); // Show the dialog. if (result == DialogResult.OK) // Test result. { string file = openFileDialog1.FileName; try { using (var reader = new StreamReader(file)) { using (var writer = new StreamWriter("results.txt")) { string currentNumber; while ((currentNumber = reader.ReadLine()) != null) { if (IsNumberValid(currentNumber)) writer.WriteLine(String.Format("{0} true", currentNumber)); } } } } catch (IOException) { } } } public bool IsNumberValid(string number) { //Whatever code you use to check your number }
这篇关于打开文本文件,通过内容和检查对环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!