Open text file, loop through contents and check against

后端 未结 5 2231
清酒与你
清酒与你 2020-12-21 17:24

So I have a generic number check that I am trying to implement:

    public static bool isNumberValid(string Number)
    {
    }

And I want to

5条回答
  •  南方客
    南方客 (楼主)
    2020-12-21 17:52

    You could try something like this:

    FileStream fsIn = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read);
    using (StreamReader sr = new StreamReader(fsIn))
     {
    
         line = sr.ReadLine();
    
         while (!String.IsNullOrEmpty(line)
         {
            line = sr.ReadLine();
           //call isNumberValid on each line, store results to list
         }
     }
    

    Then print the list using FileStream.

    As other people have mentioned, your isNumberValid method could make use of the Int32.TryParse method, but since you said your text file only contains numbers this may not be necessary. If you're just trying to match the number exactly, you can use number == line.

提交回复
热议问题