Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <locale.h>
- #include <windows.h>
- #define n 80
- int main() {
- setlocale(LC_ALL, "Rus");
- HANDLE hFile;
- DWORD dwBytesRead, dwBytesWritten;
- char s[n], sx[n], *p, scount[n];
- hFile = CreateFile("text.txt", GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
- if (hFile == INVALID_HANDLE_VALUE) {
- printf("Не удалось открыть файл\n");
- return 1;
- }
- HANDLE hTempFile = CreateFile("temp.txt", GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
- if (hTempFile == INVALID_HANDLE_VALUE) {
- printf("Не удалось создать файл\n");
- CloseHandle(hFile);
- return 2;
- }
- while (ReadFile(hFile, s, n, &dwBytesRead, NULL) && dwBytesRead > 0) {
- s[dwBytesRead] = '\0'; // Null-terminate the string read from file
- strcpy(sx, s); // Create a copy of the string
- p = strtok(s, " ");
- int count = 0;
- while (p != NULL) {
- count++;
- p = strtok(NULL, " ");
- }
- sprintf(scount, "(%d) ", count);
- p = strchr(sx, '\n');
- if (p)
- *p = ' ';
- strcat(sx, scount);
- sx[strlen(sx) - 1] = '\n';
- WriteFile(hTempFile, sx, strlen(sx), &dwBytesWritten, NULL);
- }
- CloseHandle(hFile);
- CloseHandle(hTempFile);
- DeleteFile("text.txt");
- MoveFile("temp.txt", "text.txt");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement