2015-05-05 128 views
-2

我在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"); 
     } 
    } 
} 

我真的很高兴在这件事上给了一些帮助! :)

在此先感谢!

+1

的问题是不完整的,一些重要的话是失踪。二进制输出将包含字节。每个字节在一个单独的字节中?作为最不重要的半字节? – DrKoch

+0

所以你已经完成了所有不属于任务的内容,并且想知道如何完成这项任务?因为在任务中我没有看到有关用户界面的任何信息(并且当我做这些任务时,我通常会创建命令行工具来执行这些任务) – xanatos

+0

这不会帮助向我们展示内衬:'MessageBox.Show( “STAAAAAAAAAAAAAAAHHHHHPPPPPPPP!”);'请删除这个** LOUD NOISE ** – DrKoch

回答

-1

这里有两种方法,第一种是分割,第二种是合并。请注意,您的任务是不明确的,如果你有分裂与否后第二四位转移......要清楚:

0xFF 

,我应该把它拆分到:

0x0F 0xF0 

或到

0x0F 0x0F 

我选择第一个。

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); 
     } 
    } 
} 

这样使用它:

ReadSplitWrite("inputfile.txt", "output.dat"); 
ReadMergeWrite("output.dat", "inputfile2.txt"); 

如果你看一下例子,你会看到,我阅读的8192分之4096字节块中的文件在同一时间(inputBuffer),然后我有第二个缓冲区,我把我写的拆分/合并的字节。

+0

起初它不会识别文件。所以我添加了使用System.IO;在我的Form1的顶部。我做了一个关于ReadSplitWrite的按钮,并告诉我它没有找到“inputfile.txt”。我必须手动创建文件吗? PS:我会用你的代码编辑当前的代码,使其更清晰。 – Skynzor

+0

@Skynzor你必须用你的文件名替换字符串 – xanatos

+0

我已经将inputfile.txt添加到我的obj/debug文件夹中并且在'test'中。现在,当我使用我为它创建的按钮时,它确实会生成inputfile2.txt和output.dat。现在出现我的第二个问题,这样做的目的是什么? :)我衷心地感谢你帮助我解决这个问题。我不能投票给你,因为我缺乏声望点:( – Skynzor

0

我尝试了一些东西,这就是我想出的解决问题的方法。

private void openAndSaveAsBinaryToolStripMenuItem_Click(object sender, EventArgs e) 
    { 

     string dirPath = @"C:\"; 

     //read from folder: C:\ 
     //create directory if it doesn't exist 

     if (!Directory.Exists(dirPath)) 
     { 
      Directory.CreateDirectory(dirPath); 
     } 

     string fileName = @dirPath + "/TestFile.bin"; 

     //Create binary file if it doesn't exist 
     if (!File.Exists(fileName)) 
     { 
      //File doesn't exist - create file 
      FileStream fs = new FileStream(fileName, FileMode.CreateNew); 
      BinaryWriter bw = new BinaryWriter(fs); 
      byte[] byteArray = { 0x48, 0x45, 0x4C, 0x4C, 0x4F }; //HELLO! 

      for (int i = 0; i < byteArray.Length; i++) 
      { 
       bw.Write(byteArray[i]); 
      } 

      bw.Close(); 
      fs.Close(); 

     } 

     // reads back 

     FileStream fsRead = new FileStream(fileName, FileMode.Open); 
     BinaryReader br = new BinaryReader(fsRead); 

     for (int i = 0; i < fsRead.Length; i++) 
     { 
      MessageBox.Show(br.ReadByte().ToString()); 
     } 

     br.Close(); 
     fsRead.Close(); 
    } 

它做了什么:它创建一个新的目录,如果它不存在。此外,它现在写入.bin文件并将其作为二进制文件返回给我。 (转换.ToString())。之后,它将其保存在目录中,并将其转换为可读的文本。

我认为这是作业从我这里问的。

我要感谢你们这些人的帮助,没有它,我不能这样做吧:)

+0

你的解决方案中的半字节在哪里? –

+0

@亨克:我不知道什么是半字节,因为你问了这个问题,我查了一下,但没有。必须做更多的研究。谢谢你让我知道! – Skynzor