如何从包含点阵字符集的文本文件中提取字符?

时间:2018-02-19 12:09:01

标签: c# dot-matrix

我有一个带有此文本的文件gg.txt:

L = 4
H = 5
T = E
 #  ##   ## ##  ### ###  ## # # ###  ## # # #   # # ###  #  ##   #  ##   ## ### # # # # # # # # # # ### ### 
# # # # #   # # #   #   #   # #  #    # # # #   ### # # # # # # # # # # #    #  # # # # # # # # # #   #   # 
### ##  #   # # ##  ##  # # ###  #    # ##  #   ### # # # # ##  # # ##   #   #  # # # # ###  #   #   #   ## 
# # # # #   # # #   #   # # # #  #  # # # # #   # # # # # # #    ## # #   #  #  # # # # ### # #  #  #       
# # ##   ## ##  ### #    ## # # ###  #  # # ### # # # #  #  #     # # # ##   #  ###  #  # # # #  #  ###  #  

如你所见,这是一个字母表。 L是字符长度; H是字符高度。 (他们将始终保持4和5.)T是我想要打印的角色。因此,如果T = E,我需要在控制台中仅打印E的图像。

### 
#   
##  
#   
### 

如果我要在txt中更改T,例如更改为B,程序将打印B.我没有任何想法如何执行此操作或启动,这是我的起始代码:

class Program
{
    static void Main(string[] args)
    {
        string[] fileContent = File.ReadAllLines("gg.txt");

        Console.WriteLine(string.Join("", fileContent));
        Console.ReadKey();
    }
}

3 个答案:

答案 0 :(得分:1)

请注意上面的代码根本没有错误检查。

using (var sr = new StreamReader(@"c:\gg.txt"))
{
    var l = int.Parse(sr.ReadLine().Split('=')[1]);
    var h = int.Parse(sr.ReadLine().Split('=')[1]);
    var t = sr.ReadLine().Split('=')[1].Trim()[0];
    int pos = t - 'A';

    for (int i = 0; i < h; i++)
    {
        var line = sr.ReadLine();
        Console.WriteLine(line.Substring(l*pos,l));
    }
}

请在此处查看完整代码:https://dotnetfiddle.net/FuM5ci

答案 1 :(得分:0)

如果我理解正确,你想只保留一个给定字符的子串,所以如果T是一个字符,文件中没有别的东西比你显示的那样:

int charIndex = (int)T - (int)'A';
int startPos = charIndex * L;

foreach (string line in File.ReadLines("gg.txt"))
{
    Console.WriteLine(line.Substring(startPos, L));
}

请注意,您可能希望在执行此操作之前验证T和L.

答案 2 :(得分:0)

基于之前的解决方案,我设法扩展到多个字母(不是最优雅的方式,但它的工作原理)

enter image description here

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json;
using System.Net;
using System.IO;
using System.Threading;
using System.Globalization;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            string line;
            int rowCount = 0;


            using (StreamReader sr = new StreamReader(@"gg.txt"))
            {
                var charLenght = int.Parse(sr.ReadLine().Split('=')[1]);
                var charHeight = int.Parse(sr.ReadLine().Split('=')[1]);
                var whatToPrint = sr.ReadLine().Split('=')[1].Trim();
                //foreach (var letter in whatToPrint)
                for (int i = 0; i < whatToPrint.Length; i++)

                {
                    int pos = whatToPrint[i] - 'A';

                    for (int j = 0; j < charHeight; j++)
                    {

                        line = sr.ReadLine();
                        Console.SetCursorPosition(charLenght * i, j);
                        int left = Console.CursorLeft, top = Console.CursorTop; left = left; top = top;
                        if (whatToPrint[i] == (char)32)
                            break;

                        else
                            Console.WriteLine(line.Substring(charLenght * pos, charLenght)); 


                    }
                    // back to start of the text file and skip the first 3 rows
                    sr.DiscardBufferedData();
                    sr.BaseStream.Seek(0, SeekOrigin.Begin);


                    for (int l = 0; l < 3; l++)
                    {
                        sr.ReadLine();
                    }

                }
            }

            Console.ReadLine();

        }
    }