binaryfile.cpp

来自「C++ Source code from a tutorial」· C++ 代码 · 共 49 行

CPP
49
字号
// This is the main project file for VC++ application project 
// generated using an Application Wizard.

#include "stdafx.h"

#using <mscorlib.dll>
#include <tchar.h>

using namespace System;
using namespace System::IO;

void WriteToFile() {
    FileStream *strwrite = File::OpenWrite(
        "c:\\DotNetBinary.dat");

    Byte bytearray[] = {1,2,3,4,5,6};
    strwrite->WriteByte(bytearray->Length);
    strwrite->Write(bytearray, 0, bytearray->Length);

    Byte bytearray2[] = {10,20,30};
    strwrite->WriteByte(bytearray2->Length);
    strwrite->Write(bytearray2, 0, bytearray2->Length);

    strwrite->Close();
}

void ReadFromFile() {
    FileStream *strread = File::OpenRead(
        "c:\\DotNetBinary.dat");
    Int32 inbyte; // ReadByte casts byte to int.
    Byte bytearray[];
    while ( (inbyte = strread->ReadByte()) != -1) {
        bytearray = new Byte[inbyte];
        strread->Read(bytearray, 0, inbyte);
        Console::WriteLine("Read an array:");
        for (int i=0; i<inbyte; i++) {
            Console::WriteLine(bytearray[i]);
        }
    }
    strread->Close();
}

// This is the entry point for this application
int _tmain(void)
{
    WriteToFile();
    ReadFromFile();
    return 0;
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?