Advertisement
J3st3rs_j0k3

lw7_1 for MKT2

Mar 19th, 2024
326
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.54 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <locale.h>
  5. #include <windows.h>
  6.  
  7. #define n 80
  8.  
  9. int main() {
  10.     setlocale(LC_ALL, "Rus");
  11.     HANDLE hFile;
  12.     DWORD dwBytesRead, dwBytesWritten;
  13.     char s[n], sx[n], *p, scount[n];
  14.    
  15.     hFile = CreateFile("text.txt", GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
  16.     if (hFile == INVALID_HANDLE_VALUE) {
  17.         printf("Не удалось открыть файл\n");
  18.         return 1;
  19.     }
  20.  
  21.     HANDLE hTempFile = CreateFile("temp.txt", GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
  22.     if (hTempFile == INVALID_HANDLE_VALUE) {
  23.         printf("Не удалось создать файл\n");
  24.         CloseHandle(hFile);
  25.         return 2;
  26.     }
  27.  
  28.     while (ReadFile(hFile, s, n, &dwBytesRead, NULL) && dwBytesRead > 0) {
  29.         s[dwBytesRead] = '\0'; // Null-terminate the string read from file
  30.         strcpy(sx, s); // Create a copy of the string
  31.        
  32.         p = strtok(s, " ");
  33.         int count = 0;
  34.         while (p != NULL) {
  35.             count++;
  36.             p = strtok(NULL, " ");
  37.         }
  38.         sprintf(scount, "(%d) ", count);
  39.  
  40.         p = strchr(sx, '\n');
  41.         if (p)
  42.             *p = ' ';
  43.         strcat(sx, scount);
  44.         sx[strlen(sx) - 1] = '\n';
  45.  
  46.         WriteFile(hTempFile, sx, strlen(sx), &dwBytesWritten, NULL);
  47.     }
  48.  
  49.     CloseHandle(hFile);
  50.     CloseHandle(hTempFile);
  51.     DeleteFile("text.txt");
  52.     MoveFile("temp.txt", "text.txt");
  53.  
  54.     return 0;
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement