Пользователь
Статус
Оффлайн
Регистрация
28 Июн 2020
Сообщения
366
Реакции[?]
56
Поинты[?]
1K
как в файле найти определённые байты и заменить их на другие
 
Олдфаг
Статус
Оффлайн
Регистрация
18 Фев 2019
Сообщения
2,826
Реакции[?]
1,853
Поинты[?]
24K
C++:
#include <fstream>

using namespace std;

int main() {
    ofstream f("text.txt"); // Файл и текст для теста
    f.write("hello world", 12); // str, size
    f.close();

    f.open("text.txt", ofstream::binary | ofstream::out | ofstream::in);
    f.seekp(2); // Индекс начала замены
    f.write("ugame", 5); // newStr, size
}
 
Начинающий
Статус
Оффлайн
Регистрация
16 Мар 2019
Сообщения
14
Реакции[?]
3
Поинты[?]
0
Если не слишком большой файл

C++:
#include <Windows.h>
#include <stdio.h>

BYTE* find_needle(BYTE* haystack, size_t haystack_length, BYTE* needle, size_t needle_length) {
    for (size_t haystack_index = 0; haystack_index < haystack_length; haystack_index++) {

        bool needle_found = true;
        for (size_t needle_index = 0; needle_index < needle_length; needle_index++) {
            BYTE haystack_character = haystack[haystack_index + needle_index];
            BYTE needle_character = needle[needle_index];
            if (haystack_character == needle_character) {
                continue;
            }
            else {
                needle_found = false;
                break;
            }
        }

        if (needle_found) {
            return &haystack[haystack_index];
        }
    }

    return nullptr;
}

int main(int argc, char* argv[])
{
    HANDLE hInFile = CreateFileA("input file", GENERIC_READ, FILE_SHARE_READ, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
    HANDLE hOutFile = CreateFileA("output file", GENERIC_WRITE, FILE_SHARE_READ, 0, CREATE_NEW, FILE_ATTRIBUTE_NORMAL, 0);

    DWORD dwSize = GetFileSize(hInFile, 0);

    BYTE* mem = (BYTE*)malloc(dwSize);

    DWORD dw;
    ReadFile(hInFile, mem, dwSize, &dw, 0);

    BYTE* to_be_found = (BYTE*)"bytes to be found in input file";
    BYTE* bytesOffset = find_needle(mem, dwSize, to_be_found, sizeof(to_be_found));
    printf("bytes found: %p\n", bytesOffset);

    BYTE* replace = (BYTE*)"replace with this bytes";
    memcpy(bytesOffset, replace, sizeof(replace));

    WriteFile(hOutFile, mem, dwSize, &dw, 0);

    CloseHandle(hOutFile);
    CloseHandle(hInFile);

    return 0;
}
 
Участник
Статус
Оффлайн
Регистрация
28 Дек 2018
Сообщения
518
Реакции[?]
194
Поинты[?]
33K
Если не слишком большой файл

C++:
#include <Windows.h>
#include <stdio.h>

BYTE* find_needle(BYTE* haystack, size_t haystack_length, BYTE* needle, size_t needle_length) {
    for (size_t haystack_index = 0; haystack_index < haystack_length; haystack_index++) {

        bool needle_found = true;
        for (size_t needle_index = 0; needle_index < needle_length; needle_index++) {
            BYTE haystack_character = haystack[haystack_index + needle_index];
            BYTE needle_character = needle[needle_index];
            if (haystack_character == needle_character) {
                continue;
            }
            else {
                needle_found = false;
                break;
            }
        }

        if (needle_found) {
            return &haystack[haystack_index];
        }
    }

    return nullptr;
}

int main(int argc, char* argv[])
{
    HANDLE hInFile = CreateFileA("input file", GENERIC_READ, FILE_SHARE_READ, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
    HANDLE hOutFile = CreateFileA("output file", GENERIC_WRITE, FILE_SHARE_READ, 0, CREATE_NEW, FILE_ATTRIBUTE_NORMAL, 0);

    DWORD dwSize = GetFileSize(hInFile, 0);

    BYTE* mem = (BYTE*)malloc(dwSize);

    DWORD dw;
    ReadFile(hInFile, mem, dwSize, &dw, 0);

    BYTE* to_be_found = (BYTE*)"bytes to be found in input file";
    BYTE* bytesOffset = find_needle(mem, dwSize, to_be_found, sizeof(to_be_found));
    printf("bytes found: %p\n", bytesOffset);

    BYTE* replace = (BYTE*)"replace with this bytes";
    memcpy(bytesOffset, replace, sizeof(replace));

    WriteFile(hOutFile, mem, dwSize, &dw, 0);

    CloseHandle(hOutFile);
    CloseHandle(hInFile);

    return 0;
}
ебать оверкомпликейт...
 
Пользователь
Статус
Оффлайн
Регистрация
28 Июн 2020
Сообщения
366
Реакции[?]
56
Поинты[?]
1K
Если не слишком большой файл

C++:
#include <Windows.h>
#include <stdio.h>

BYTE* find_needle(BYTE* haystack, size_t haystack_length, BYTE* needle, size_t needle_length) {
    for (size_t haystack_index = 0; haystack_index < haystack_length; haystack_index++) {

        bool needle_found = true;
        for (size_t needle_index = 0; needle_index < needle_length; needle_index++) {
            BYTE haystack_character = haystack[haystack_index + needle_index];
            BYTE needle_character = needle[needle_index];
            if (haystack_character == needle_character) {
                continue;
            }
            else {
                needle_found = false;
                break;
            }
        }

        if (needle_found) {
            return &haystack[haystack_index];
        }
    }

    return nullptr;
}

int main(int argc, char* argv[])
{
    HANDLE hInFile = CreateFileA("input file", GENERIC_READ, FILE_SHARE_READ, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
    HANDLE hOutFile = CreateFileA("output file", GENERIC_WRITE, FILE_SHARE_READ, 0, CREATE_NEW, FILE_ATTRIBUTE_NORMAL, 0);

    DWORD dwSize = GetFileSize(hInFile, 0);

    BYTE* mem = (BYTE*)malloc(dwSize);

    DWORD dw;
    ReadFile(hInFile, mem, dwSize, &dw, 0);

    BYTE* to_be_found = (BYTE*)"bytes to be found in input file";
    BYTE* bytesOffset = find_needle(mem, dwSize, to_be_found, sizeof(to_be_found));
    printf("bytes found: %p\n", bytesOffset);

    BYTE* replace = (BYTE*)"replace with this bytes";
    memcpy(bytesOffset, replace, sizeof(replace));

    WriteFile(hOutFile, mem, dwSize, &dw, 0);

    CloseHandle(hOutFile);
    CloseHandle(hInFile);

    return 0;
}
ну 32 мб ,но я могу сделать по другому спасибо
 
Сверху Снизу