Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #define MAX_WORD_LENGTH 100
- void removeSectionBetweenWords(FILE *inputFile, FILE *outputFile, const char *word1, const char *word2) {
- char buffer[MAX_WORD_LENGTH * 2 + 1]; // Буфер для хранения слова и пробелов
- int foundWord1 = 0;
- while (fgets(buffer, sizeof(buffer), inputFile)) {
- char *p = buffer;
- // Пока не найдем первое слово или не дойдем до конца строки
- while (*p && !foundWord1) {
- if (strcmp(p, word1) == 0) {
- foundWord1 = 1;
- p += strlen(word1); // Пропускаем первое слово
- continue;
- }
- fputc(*p, outputFile);
- p++;
- }
- // После нахождения первого слова пропускаем все до второго
- while (*p && strcmp(p, word2) != 0) {
- p++;
- }
- // Если нашли второе слово, записываем оставшуюся часть строки
- if (*p && strcmp(p, word2) == 0) {
- fputc(*p, outputFile);
- p++;
- while (*p) {
- fputc(*p, outputFile);
- p++;
- }
- }
- }
- }
- int main(int argc, char *argv[]) {
- if (argc != 4) {
- printf("Usage: %s input_file output_file word1 word2\n", argv[0]);
- return 1;
- }
- const char *inputFileName = argv[1];
- const char *outputFileName = argv[2];
- const char *word1 = argv[3];
- const char *word2 = argv[4];
- FILE *inputFile = fopen(inputFileName, "r");
- if (inputFile == NULL) {
- perror("Error opening input file");
- return 1;
- }
- FILE *outputFile = fopen(outputFileName, "w");
- if (outputFile == NULL) {
- perror("Error opening output file");
- fclose(inputFile);
- return 1;
- }
- removeSectionBetweenWords(inputFile, outputFile, word1, word2);
- fclose(inputFile);
- fclose(outputFile);
- printf("Section between '%s' and '%s' removed successfully.\n", word1, word2);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement