Тема: Робота з бінарними файлами, як зчитати масив чисел
Я хочу зчитати масив чисел, розміром 256*1024 елементів, але я не знаю як його зчитати за один раз
Зробив так:
зчитую число розміром unsigned int і записую їх в num 
readFile.read((char*)&num, sizeof(unsigned int));коли num отримав число, то sprintf_s його конвертує в рядок char
sprintf_s(tempNum.get(), nod,"%u", num)nod - це максимальна кількість цифр (в даному випадку 10)
потім strcat_s копіює tempNum в рядок contents, (в contents має бути все число розміром 1 мб з файлу)
strcat_s(contents.get(), memory1mb, tempNum.get());після копіювання очищається число num, і в freeMemory присвоюється кількість вільної пам'яті
num = 0;/*clear*/
freeMemory = (size_t)memory1mb - strlen(contents.get());/*calculate the memory*/Якщо пам'яті достатньо і з файлом нічого не сталось (наприклад файл закінчився, або сталась помилка), повторюється все з початку
while (readFile.good() && freeMemory > nod);Ось весь код
#include <stdio.h> // sprintf_s
#include <iostream> // cout
#include <fstream> // ifstream
#include <climits> // UINT_MAX
#include <cstring> // strcpy_s strcat_s strlen
#include <memory> // unique_ptr
#include <exception> // exception
using namespace std;
template <typename T>
T numberOfDigits(T num)
{
/*
This function count number of digits
For example:
in 1240 -> out 4
*/
    unsigned short number_of_digits(0);
    do
    {
        ++number_of_digits;
        num /= 10;
    } while (num);
    return number_of_digits;
}
unsigned short numberOfDigitsInUI()
{
/*
This function returns the maximum number of digits in an unsigned integer
For example:
unsigned integer has 4294967295 maximum number then this function returns 10
*/
    return numberOfDigits(UINT_MAX);
}
void readBinaryFile(char *path/*in*/, unique_ptr<char[]> &contents/*out*/)
{
/*
This function reads a number from the file 
on the path "path", and then returns back with "contents"
*/
    ifstream readFile;/*file stream*/
    readFile.open(path, ios::in | ios::binary);/*open file in the binary mode*/
    unsigned short nod(numberOfDigitsInUI() + 1);/*(nod - number of digits) the maximum number of digits in an unsigned integer*/
    unsigned int memory1mb = 262145;// 262144 + 1
    unsigned int num(0);/*read numbers from a file at a time.*/
    contents = make_unique<char[]>(memory1mb);/*1 mb data read from a file*/
    auto tempNum = make_unique<char[]>(nod);/*the maximum amount of data which fits in an unsigned integer*/
    strcpy_s(contents.get(), memory1mb, "");/*clear*/
    strcpy_s(tempNum.get(), nod, "");/*clear*/
    if (readFile.is_open() && !readFile.eof())/*if file is open and file is not empty*/
    {
        size_t freeMemory;
        do
        {
            readFile.read((char*)&num, sizeof(unsigned int));
            if (0 > sprintf_s(tempNum.get(), nod,"%u", num))
            {
                throw exception("Error! Cannot converting.");
            }
            strcat_s(contents.get(), memory1mb, tempNum.get());
            num = 0;/*clear*/
            freeMemory = (size_t)memory1mb - strlen(contents.get());/*calculate the memory*/
        } while (readFile.good() && freeMemory > nod);
    }
    else
    {
        throw exception("Error! Cannot open file.");
    }
    readFile.close(); 
}
int main(int argc, char *argv[])
{
    try
    {
        unique_ptr<char[]> read;
        readBinaryFile("A:\\3.txt", read);
        cout << read.get();
    }
    catch (exception &e)
    {
        cout << e.what();
    }
    
    getchar();
    return 0;
}Ще раз напишу завдання
Зчитати з бінарного файлу число розміром 1 мб за один раз, і конвертувати його в рядок char.
Як це правильно зробити ?