#include <windows.h>
#include <iostream>
bool linkFiles(const char* file1, const char* file2, const char* outputFile) {
HANDLE hFile1 = CreateFile(file1, GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
HANDLE hFile2 = CreateFile(file2, GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
HANDLE hOutputFile = CreateFile(outputFile, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
if (hFile1 == INVALID_HANDLE_VALUE || hFile2 == INVALID_HANDLE_VALUE || hOutputFile == INVALID_HANDLE_VALUE) {
std::cerr << "Error opening files." << std::endl;
return false;
}
char buffer[4096];
DWORD bytesRead;
DWORD bytesWritten;
// Leer del primer archivo y escribir en el archivo de salida
while (ReadFile(hFile1, buffer, sizeof(buffer), &bytesRead, NULL) && bytesRead > 0) {
WriteFile(hOutputFile, buffer, bytesRead, &bytesWritten, NULL);
}
// Leer del segundo archivo y escribir en el archivo de salida
while (ReadFile(hFile2, buffer, sizeof(buffer), &bytesRead, NULL) && bytesRead > 0) {
WriteFile(hOutputFile, buffer, bytesRead, &bytesWritten, NULL);
}
// Cerrar los handles
CloseHandle(hFile1);
CloseHandle(hFile2);
CloseHandle(hOutputFile);
return true;
}
int main() {
const char* file1 = "archivo1.txt";
const char* file2 = "archivo2.txt";
const char* outputFile = "archivo_unido.txt";
if (linkFiles(file1, file2, outputFile)) {
std::cout << "Archivos unidos exitosamente." << std::endl;
} else {
std::cout << "Error al unir los archivos." << std::endl;
}
return 0;
}