In general, a line is given in the file, the task is to print all the words that occur in the sequence once.
I now delete the re-entering words, that is, if the content in the file
qwert, qw, qwert, qaz, qwert, qaz, qwe.
She gives out
qwert, qw, qaz, qwe.
This is not something I need to give out.
qw, qwe.
#include <string.h> #include <stdio.h> #include <conio.h> #include <io.h> #include <stdlib.h> #include <string.h> int main() { FILE *f1, *f2; //2 ΡΠ°ΠΉΠ»Π° char nameIn[20]; //ΠΈΠΌΡ ΡΠ°ΠΉΠ»Π°1(Π΄Π»ΠΈΠ½Π°) char nameOut[20]; //ΠΈΠΌΡ ΡΠ°ΠΉΠ»Π°2(Π΄Π»ΠΈΠ½Π°) char source[256]; char dest[256] = { '\0' }; char *buf; char *s; int size; printf("Vvedite imy faila: "); //ΡΠΏΡΠ°ΡΠΈΠ²Π°Π΅ΡΡ ΠΈΠΌΡ scanf("%s", nameIn); //ΡΡΠΈΡΡΠ²Π°Π΅ΡΡ ΠΈΠΌΡ f1 = fopen(nameIn, "r"); //ΠΎΡΠΊΡΡΠ²Π°Π΅ΠΌ ΡΠ°ΠΉΠ» 1 while (f1 == NULL) //ΡΠΈΠΊΠ» ΠΎΡ ΠΎΡΠΈΠ±ΠΎΠΊ,Π΅ΡΠ»ΠΈ Π½Π΅ ΡΡΡΠ΅Π²ΡΡΠ²ΡΠ΅Ρ ΡΠ°ΠΉΠ»Π°1 { printf("tError!n"); printf("Vvedite imy faila: "); //ΡΠΏΡΠ°ΡΠΈΠ²Π°Π΅ΡΡ ΠΈΠΌΡ scanf("%s", nameIn); //ΡΡΠΈΡΡΠ²Π°Π΅ΡΡ ΠΈΠΌΡ f1 = fopen(nameIn, "r"); //ΠΎΡΠΊΡΡΡΠΈΠ΅ ΡΠ°ΠΉΠ»Π°1 } size = filelength(fileno(f1)); //ΠΏΠΎΠ»ΡΡΠ°Π΅ΠΌ ΡΠ°Π·ΠΌΠ΅Ρ ΡΠ°ΠΉΠ»Π° int readsize = fread(source, sizeof(char), sizeof(char) * size, f1); //ΡΠΈΡΠ°ΡΡ ΡΡΡΠΎΠΊΡ ΠΈΠ· ΡΠ°ΠΉΠ»Π° ΠΏΠΎ ΡΠΈΠΌΠ²ΠΎΠ»Π°ΠΌ.Π² ΠΏΠ΅ΡΠ΅ΠΌΠ΅Π½Π½ΡΡ readsize Π·Π°ΠΏΠΈΡΡΠ²Π°Π΅ΡΡΡ ΡΠ°Π·ΠΌΠ΅Ρ buf = strtok(source, ",."); while (buf != '\0') { s = strstr(dest, buf); //Π€ΡΠ½ΠΊΡΠΈΡ strstr() Π²ΠΎΠ·Π²ΡΠ°ΡΠ°Π΅Ρ ΡΠΊΠ°Π·Π°ΡΠ΅Π»Ρ Π½Π° ΠΏΠ΅ΡΠ²ΠΎΠ΅ Π²Ρ
ΠΎΠΆΠ΄Π΅Π½ΠΈΠ΅ ΠΏΠΎΠ΄ΡΡΡΠΎΠΊΠΈ, Π°Π΄ΡΠ΅ΡΡΠ΅ΠΌΠΎΠΉ ΠΏΠ°ΡΠ°ΠΌΠ΅ΡΡΠΎΠΌ str2, Π² ΡΡΡΠΎΠΊΡ, Π°Π΄ΡΠ΅ΡΡΠ΅ΠΌΡΡ ΠΏΠ°ΡΠ°ΠΌΠ΅ΡΡΠΎΠΌ str1. ΠΡΠ»ΠΈ ΡΠΎΠ²ΠΏΠ°Π΄Π΅Π½ΠΈΠ΅ Π½Π΅ ΠΎΠ±Π½Π°ΡΡΠΆΠ΅Π½ΠΎ, Π²ΠΎΠ·Π²ΡΠ°ΡΠ°Π΅ΡΡΡ Π½ΡΠ»Π΅Π²ΠΎΠΉ ΡΠΊΠ°Π·Π°ΡΠ΅Π»Ρ. if (!s || *(s + strlen(buf)) != ',') { strcat(dest, buf); //ΠΏΡΠΈΡΠΎΠ΅Π΄ΠΈΠ½ΡΠ΅Ρ ΠΊ ΡΡΡΠΎΠΊΠ΅ str1 ΠΊΠΎΠΏΠΈΡ ΡΡΡΠΎΠΊΠΈ str2 strcat(dest, ","); //ΠΏΡΠΈΡΠΎΠ΅Π΄ΠΈΠ½ΡΠ΅Ρ ΠΊ ΡΡΡΠΎΠΊΠ΅ str1 ΠΊΠΎΠΏΠΈΡ ΡΡΡΠΎΠΊΠΈ str2 } buf = strtok(0, ",."); } dest[strlen(dest) - 1] = '.'; printf("Vvedite imy vihodnogo faila: "); //ΡΠΏΡΠ°ΡΠΈΠ²Π°Π΅ΡΡ ΠΈΠΌΡ scanf("%s", nameOut); //ΡΡΠΈΡΡΠ²Π°Π΅ΡΡ ΠΈΠΌΡ f2 = fopen(nameOut, "w"); //ΠΎΡΠΊΡΡΡΠΈΠ΅ ΡΠ°ΠΉΠ»Π°2 printf("n"); printf("tRezultat izmeneniy: n"); printf("%s", dest); fprintf(f2, "%s", dest); fclose(f2); //Π·Π°ΠΊΡΡΡΠΈΠ΅ ΡΠ°ΠΉΠ»Π°2 fclose(f1); //Π·Π°ΠΊΡΡΡΠΈΠ΅ ΡΠ°ΠΉΠ»Π°1 getch(); }