Читать большой файл байтами и записывать в другой - C#
Формулировка задачи:
потехоньку перехожу с дельфи на си диз подскажите как прочитать файл по байтам в массив размером 30 метров. сам делаю чеа лажа какаято непонятно. вот примерчик который получился.
он должен прочитать файл и записать в другой файл в качестве промежутка выступает массв
я пример не весь выложил тока часть чтения
FileStream w1 = new FileStream(per1, FileMode.Open); byte c; while (w1.ReadByte() != -1) { c = (byte)w1.ReadByte(); buf1[index] = c; index = index + 1; if (index == max) { //буфер переполнен - тогда записать в файл ТоФайл(); index = 0; } } MessageBox.Show(index.ToString()); w1.Close();
Решение задачи: «Читать большой файл байтами и записывать в другой»
textual
Листинг программы
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.IO; namespace Обьеденение_файлов { public partial class Form1 : Form { public Form1() { InitializeComponent(); } int max = 8*1024*1024; // 30МБ byte[] buf1; int index = 0; void ТоФайл() { //весь каталог прочитан и буфер заполнен! его надо записать в файл const string namf = "Date.cab"; if (File.Exists(namf) == true) ; FileStream e2; e2 = new FileStream(namf, FileMode.Append, FileAccess.Write); e2.Write(buf1, 0, index); e2.Flush(); e2.Close(); } private void button1_Click(object sender, EventArgs e) { buf1 = new byte[max]; //читаем список файлов const string file = "catalog.txt"; if (File.Exists(file) == false) { return; } StreamReader text1 = new StreamReader(file); string per1; while ((per1 = text1.ReadLine()) != null) { if (File.Exists(per1)) { FileStream nf = new FileStream(per1, FileMode.Open); BinaryReader n = new BinaryReader(nf); //byte c; int x = 0; while (nf.Position <= nf.Length) { x++; label1.Text = ToString(); buf1[inadex] = n.ReadByte(); index ++; if (index == max) { ТоФайл(); index = 0; } } n.Close(); nf.Close(); } } text1.Close(); MessageBox.Show("OK"); } } }
ИИ поможет Вам:
- решить любую задачу по программированию
- объяснить код
- расставить комментарии в коде
- и т.д