试图记录读取器在数组中的位置

本文关键字:数组 位置 记录 读取 | 更新日期: 2023-09-27 18:08:44

这个程序显示文本块来帮助用户记忆。每次用户按下一个按钮,就会显示另一大块文本。我在记录标点符号的位置。当用户按下"减少文本"按钮时,一大块文本将被删除……即. .

用户按检索文本3次

标签显示....我去了,到了那所房子,它很好。

用户按下'less text'一次;

标签显示.....我去了,去了房子,

我的问题是'less text'按钮。我希望该按钮能够检查已存储在数组中的读者位置,然后显示文本,直到第二个最后一个标点符号记录在数组中。我觉得我使用BaseStream的方式。位置不正确。我似乎找不到如何记录阅读器对象的位置的清晰度。谢谢你。

公共部分类frmMainWindow: Form{公共frmMainWindow (){InitializeComponent ();}

    // Creates an object that we can append values to and create a string
    StringBuilder textToMemorize = new StringBuilder();
    // Reads in text from a file. This is the text that the user will memorize
    StreamReader reader = new StreamReader(@"C:'Users'Jason'Desktop'MemorizerTestApplication'WindowsFormsApplication1'bin'Debug'Test Document.txt");

    // The point where the character is beig read by the input stream
    int readPoint;
    // And Array to contain all the places where the a puctuation mark has already stopped the displaying of more text
    long[] stopPoint = new long[20];
    // Integer to move the element in the stop point array
    int p = 0;
    // The return value for the skipPunctuation method
    bool doAppend;
    /// <summary>
    /// Handles the Click event of the retrieve text button
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void button1_Click(object sender, EventArgs e)
    {
        bool flag = true;
        // Collects each character from the text file and checks for punctuation    
        while((readPoint = reader.Read()) > 0 && flag == true)
        {
            // Casts the current letter into a character
            char c = (char)readPoint;
            // Checks for punctuation
            if (c == ',' || c == ';' || c == '.')
            {
                    // Stores the readPoint where there is a punctuation mark
                    stopPoint[p] = reader.BaseStream.Position;
                    textToMemorize.Append((char)c);
                    p++;
                    flag = false;
            }
            else
            {
                // Appends the character to the text
                textToMemorize.Append((char)c);
            }
        }
        // Displays text from the string building to the label
        lblTextToLearn.Text = textToMemorize.ToString();
     }
            private void btnLessText_Click(object sender, EventArgs e)
    {
        // Clear the label
        lblTextToLearn.Text = string.Empty;
        //Clears the String Builder object
        textToMemorize.Clear();
        // Sets the internal stream back to zero
        reader.DiscardBufferedData();
        // Sets the stream back to zero
        reader.BaseStream.Seek(0, SeekOrigin.Begin);
        bool stopLoop = true;
        // Loop the reader
        while ((readPoint = reader.Read()) > 0 && stopLoop == true)
        {
            // Cast the read point to a char
           char d = (char)readPoint;
            // Append the char to the string builder objecy
            textToMemorize.Append(d);
            // Display the string to the label
            lblTextToLearn.Text = textToMemorize.ToString();
            // CHeck the second last element of the array to know how many char's to print to the screen
            if (reader.BaseStream.Position == stopPoint[p-1])
            {
                stopLoop = false;
            }
        }

试图记录读取器在数组中的位置

显示流读取器的位置,这是一个繁重的任务

这里有其他方法:

using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
public partial class test
{
    // temp string
    String temp = string.Empty;
    // array of readed text
    char[] chars;
    // helper variable
    int found = 0;
    // compare list
    List<String> compare = new List<string>() { ",", ";", "." };
    // Stream Reader
    StreamReader reader = new StreamReader("file.txt");
    public string increaseText()
    {
        // Read next line if end of this line
        if (chars == null || (found > 0 && temp.Length >= chars.Length))
        {
            // read whole line
            chars = reader.ReadLine().ToCharArray();
        }
        // check every char
        for (int i = found; i < chars.Length; i++)
        {
            temp += chars[i].ToString();
            // if stop sign found, exit and return our string
            if (compare.Contains(chars[i].ToString()))
            {
                found = i + 1;
                break;
            }
        }
        return temp;
    }
    public string decreaseText()
    {
        // get all chars
        char[] compChars = temp.ToCharArray();
        // check every char
        for (int i = temp.Length-1; i > 0; i--)
        {
            // if stop sign found, decrease text
            if (compare.Contains(compChars[i-1].ToString()))
            {
                found = i;
                temp = temp.Substring(0, i);
                break;
            }
        }
        return temp;
    }
}

阅读每一行代码也很好,希望这有帮助:)

基本上reader.BaseStream.Position并不包含你所想的。

从文档中:Position属性不跟踪流中已被消耗、跳过或两者兼而有之的字节数。

如果你想使用一个流来做这件事,你必须自己计算所消耗的字符。