【发布时间】:2013-04-20 15:54:42
【问题描述】:
尝试根据用户变量条目从文本文件中读取
我的条目有“Big Fred”这个名字的变体(大写/小写)
代码运行,但我没有得到任何结果。
我笔记本电脑上的代码指向我已从下面的代码中删除的特定位置。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace ReadTextFileWhile
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Enter the name you wish to search for: "); //Prompt user for the name they wish to search for
string x = Console.ReadLine(); //assign the name the user inputs as their search parameter to the value of x
StreamReader myReader = new StreamReader("C:/insert location here"); //Read the the text file at this location and assign to myReader
string line = ""; //asssign 'nothing' to line.
while (line != null) //while line in the text file is not null (empty)
{
line = myReader.ReadLine(); //pass contents of myReader to line
if (line != null && line == x) //if contents of line are not null and equal to the variable in x print to screen
Console.WriteLine(line);
}
myReader.Close(); //close myReader properly
Console.ReadLine(); //Readline to keep console window open allowing a human to read output.
}
}
}
【问题讨论】:
-
我已经编辑了你的标题。请参阅“Should questions include “tags” in their titles?”,其中的共识是“不,他们不应该”。
标签: c# file variables search text