简体   繁体   中英

Open a txt file using C# and read the numbers on the file

How can I open a .txt file and read numbers separated by enters or spaces into an array list?

Example:

Now what I want to do is to search (for 1 2 9 ) and send to the console.

I have tried a lot of code but nothing seems to work :(

This is my current code :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
namespace Padroes
{
class Program
{
    static void Main(string[] args)
    {
        try
        {


            // Open the text file using a stream reader.
            const string FILENAME = @"Example.txt";


                List<List<int>> data = new List<List<int>>();

                string inputLine = "";
                StreamReader reader = new StreamReader(FILENAME);

                while ((inputLine = reader.ReadLine()) != null)
                {
                    inputLine = inputLine.Trim();
                    if (inputLine.Length > 0)
                    {
                        List<int> inputArray = inputLine.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries).Select(x => int.Parse(x)).ToList();

                        data.Add(inputArray);
                    Console.WriteLine(inputLine);
                    }
                }
            }

        catch (Exception e)
        {
            Console.WriteLine("The file could not be read:");
            Console.WriteLine(e.Message);
        }

        Console.ReadKey();
    }
}
}

With this code this is my output:

Now what can I do to search only for ( 1 2 9 ) and send only the 1 2 9 to console ?

I belive this would do the trick.. I simply used a StreamReader and looped throught each line.. Im not sure if i got the part of the condition 100% but if i do it should look somthing like this :

      StreamReader file = new StreamReader(@"test.txt");
      string line= file.ReadLine();
        while(line!=null)
        {
            if (line.Equals("5 8 1 7"))
                MessageBox.Show(line);
               line = file.ReadLine();
        }

Goodluck.

Try this

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace ConsoleApplication1
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.txt";
        static void Main(string[] args)
        {
            List<List<int>> data = new List<List<int>>();

            string inputLine = "";
            StreamReader reader = new StreamReader(FILENAME);

            while((inputLine = reader.ReadLine()) != null)
            {
                inputLine = inputLine.Trim();
                if (inputLine.Length > 0)
                {
                    List<int> inputArray = inputLine.Split(new char[] {' '}, StringSplitOptions.RemoveEmptyEntries).Select(x => int.Parse(x)).ToList();

                    data.Add(inputArray);
                }
            }
        }
    }
}
​

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM