#include
#include
#include
#include
#include
#define BUF_SIZE 256
int _tmain(int argc, LPTSTR argv[]) {
HANDLE hIn, hOut;
DWORD nIn, nOut, i, j;
CHAR Buffer[BUF_SIZE], wordi[2];
setlocale(LC_ALL, "Russian");
if (argc != 4) {
printf("Использование: cpw файл1 файл2\n");
return 1;
}
hIn = CreateFile(argv[1], GENERIC_READ, 0, NULL, OPEN_EXISTING, 0, NULL);
if (hIn == INVALID_HANDLE_VALUE) {
printf("Невозможно открыть входной файл. Ошибка: %x\n", GetLastError());
return 2;
}
hOut = CreateFile(argv[2], GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_FLAG_WRITE_THROUGH, NULL);
if (hOut == INVALID_HANDLE_VALUE) {
printf("Невозможно открыть выходной файл. Ошибка: %x\n", GetLastError());
return 3;
}
while (ReadFile(hIn, Buffer, BUF_SIZE, &nIn, NULL) && nIn > 0) {
wordi[0] = argv[3][0];
i = nIn;
for (j = 0; i - 1 > j; j++)
{
if (Buffer[j] == wordi[0])
{
Buffer[j++] = ' \n';
}
printf("%c", Buffer[j]);
SetFilePointer(hOut, 0, NULL, FILE_BEGIN);
WriteFile(hOut, Buffer, nIn, &nOut, NULL);
SetEndOfFile(hIn);
if (nIn != nOut) {
printf("Неустранимая ошибка записи: %x\n", GetLastError());
return 4;
}
}
}
CloseHandle(hIn);
CloseHandle(hOut);
return 0;
}
