我在C#中的起始学生,我无法找到以下问题的答案:C#分配,二进制文件类型
“写一个程序,打开一个文本文件,并保存在单独的高,低4位半字节和二进制文件编写一个程序来完成reerse,即从二进制文件中读取两个字节,将它们组合起来并将它们写为文本文件“
我可以读取代码,并理解它。但是因为我是这个领域的新手,所以我很难完全靠自己完成。
我已经编写了用于打开.txt文件并将其保存为.txt文件的代码。
form1的图像:因为我缺乏“声誉”,我无法发布图像。 :(
这是我写的代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
namespace _9._3_menustripAndFiledialog
{
public partial class Form1 : Form
{
private System.Drawing.Printing.PrintDocument docToPrint =
new System.Drawing.Printing.PrintDocument();
public Form1()
{
InitializeComponent();
}
private void helpToolStripMenuItem_Click(object sender, EventArgs e)
{
}
private void openToolStripMenuItem_Click(object sender, EventArgs e)
{
//Clear rich text box
richTextBox1.Clear();
//Set open file dialog initial directory and title
openFileDialog1.InitialDirectory = @"C:\"; //Hier zeg je welke directory drive hij moet openen
openFileDialog1.Title = "Please select a file";
openFileDialog1.Filter= "Text files(*.TXT)|*.txt";
MessageBox.Show("Only .txt files can be opened.");
//Open the dialog and check for cancel
if (openFileDialog1.ShowDialog() != DialogResult.Cancel)
{
// niet gecanceled - lees bestand
richTextBox1.LoadFile(openFileDialog1.FileName, RichTextBoxStreamType.PlainText);
}
else
{
MessageBox.Show("Gosh darn it! You pressed cancel!");
}
}
private void saveToolStripMenuItem_Click(object sender, EventArgs e)
{
if(saveFileDialog1.ShowDialog() != DialogResult.Cancel)
{
// niet gecanceld - schrijf het bestand
richTextBox1.SaveFile(saveFileDialog1.FileName, RichTextBoxStreamType.PlainText);
} else {
MessageBox.Show("You pressed cancel!");
}
}
private void exitToolStripMenuItem_Click(object sender, EventArgs e)
{
// Display a message box asking users if they
// want to exit the application.
if (MessageBox.Show("Do you want to exit?", "My Application",
MessageBoxButtons.YesNo, MessageBoxIcon.Question)
== DialogResult.Yes)
{
Application.Exit();
}
}
private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
{
AboutBox1 frmAbout = new AboutBox1();
frmAbout.Show();
}
private void cutToolStripMenuItem_Click(object sender, EventArgs e)
{
richTextBox1.Cut();
}
private void pasteToolStripMenuItem_Click(object sender, EventArgs e)
{
richTextBox1.Paste();
}
private void undoToolStripMenuItem_Click(object sender, EventArgs e)
{
richTextBox1.Undo();
}
private void copyToolStripMenuItem_Click(object sender, EventArgs e)
{
richTextBox1.Copy();
}
private void redoToolStripMenuItem_Click(object sender, EventArgs e)
{
richTextBox1.Redo();
}
private void findTextToolStripMenuItem_Click(object sender, EventArgs e)
{
richTextBox1.Find("Text", RichTextBoxFinds.MatchCase);
richTextBox1.SelectionFont = new Font("Verdana", 12, FontStyle.Italic);
richTextBox1.SelectionColor = Color.Blue;
}
public void replaceTextToolStripMenuItem_Click(object sender, EventArgs e)
{
richTextBox1.Text = richTextBox1.Text.Replace("Text", "newText");
}
private void printToolStripMenuItem_Click(object sender, EventArgs e)
{
printDialog1.AllowSomePages = true;
printDialog1.ShowHelp = true;
printDialog1.Document = docToPrint;
DialogResult result = printDialog1.ShowDialog();
if (result == DialogResult.OK)
{
docToPrint.Print();
}
}
//代码从这里开始从用户添加:萨那托斯
public static void ReadSplitWrite(string inputFile, string outputFile)
{
using (var sr = File.OpenRead(inputFile))
using (var sw = File.Create(outputFile))
{
int read;
byte[] inputBuffer = new byte[4096];
byte[] outputBuffer = new byte[inputBuffer.Length * 2];
while ((read = sr.Read(inputBuffer, 0, inputBuffer.Length)) != 0)
{
for (int i = 0, j = 0; i < read; i++, j += 2)
{
outputBuffer[j] = (byte)(inputBuffer[i] & 0x0F);
outputBuffer[j + 1] = (byte)(inputBuffer[i] & 0xF0);
}
sw.Write(outputBuffer, 0, read * 2);
}
}
}
public static void ReadMergeWrite(string inputFile, string outputFile)
{
using (var sr = File.OpenRead(inputFile))
using (var sw = File.Create(outputFile))
{
int read;
byte[] inputBuffer = new byte[4096 * 2];
byte[] outputBuffer = new byte[inputBuffer.Length/2];
while ((read = sr.Read(inputBuffer, 0, inputBuffer.Length)) != 0)
{
for (int i = 0, j = 0; i < read; i += 2, j++)
{
outputBuffer[j] = inputBuffer[i];
outputBuffer[j] |= inputBuffer[i + 1];
}
sw.Write(outputBuffer, 0, read/2);
}
}
}
//我做了一个自定义按钮支持读取和写入的方法,并让它在我的表格内
private void openAndSaveAsBinaryToolStripMenuItem_Click(object sender, EventArgs e)
{
ReadSplitWrite("inputfile.txt", "output.dat");
ReadMergeWrite("output.dat", "inputfile2.txt");
}
}
}
我真的很高兴在这件事上给了一些帮助! :)
在此先感谢!
的问题是不完整的,一些重要的话是失踪。二进制输出将包含字节。每个字节在一个单独的字节中?作为最不重要的半字节? – DrKoch
所以你已经完成了所有不属于任务的内容,并且想知道如何完成这项任务?因为在任务中我没有看到有关用户界面的任何信息(并且当我做这些任务时,我通常会创建命令行工具来执行这些任务) – xanatos
这不会帮助向我们展示内衬:'MessageBox.Show( “STAAAAAAAAAAAAAAAHHHHHPPPPPPPP!”);'请删除这个** LOUD NOISE ** – DrKoch