此程序显示文本块以帮助用户记忆。每次用户按下按钮时,都会显示另一块文本。 我试图记录标点符号的位置。当用户按下较少的文字时按钮删除了一大块文本......即..
用户按3次检索文本
标签显示....我去了房子,很不错。
用户按下较少的文字'一次;
标签显示.....我去了房子,
我的问题在于少了一些文字'按钮。我希望该按钮能够检查已存储在数组中的阅读器位置,然后显示文本直到阵列中记录的第二个标点符号。我觉得我使用BaseStream.Position的方式不正确。我似乎无法清楚地知道如何记录读者对象的位置。谢谢。
公共部分类frmMainWindow:表单 { public 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;
}
}
答案 0 :(得分:0)
使用流阅读器位置,这是一项艰巨的任务
这里有其他方法可以做到这一点:
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;
}
}
阅读每一行代码也行得正常,希望这有助于:)
答案 1 :(得分:0)
基本上reader.BaseStream.Position并不包含您的想法。
如果你想为此使用一个流,你必须自己统计消费的角色。