• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 354
  • Last Modified:

Reading a Text File and Counting Data C#

I have a text file that holds data like this:

The Title Of The File
1,2,4,5,U
0,0,0,0,UURR
2,2,4,5,ULL
0,1,0,0,UURR


I want to count the number of lines with data after the first line (which is the title) and store that number after I have the number of data in the file I want a mechanism to randomly choose one piece of data and display it as output (say a label or something) keep randomly choosing a piece of data until you displayed all the data once.

Using that as an example would help me with what I need to code.
0
npl77
Asked:
npl77
1 Solution
 
Expert1701Commented:
Here is a complete example.
  static void Main()
  {
    string filePath = @"C:\Data.txt";
 
    List<string> data = new List<string>();
    using (System.IO.StreamReader reader = new System.IO.StreamReader(filePath))
    {
      string line = reader.ReadLine();
      while ((line = reader.ReadLine()) != null)
        data.Add(line);
    }
 
    Random random = new Random();
 
    while (data.Count > 0)
    {
      int index = random.Next(data.Count);
      Console.WriteLine(data[index]);
      data.RemoveAt(index);
    }
 
    Console.ReadLine();
  }

Open in new window

0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now