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(); } 
  • The labels should indicate 1) a programming language, 2) a training task. - Nicolas Chabanovsky ♦
  • @Spectre, And specifically in the case can you say something? - wapdimon72ru
  • when the question is read by people who know C ++, then they will say the case - Specter
  • If you understand correctly, you need from the words (delimiters '.' And ',') of the input line to get a string of those words that enter the source one time . So ? Then your algorithm is wrong. And here the sorting and two files do not understand. - avp
  • @ wapdimon72ru, Dak, I know what is not correct, right now it just removes the repeated occurrences of words, and it’s necessary to show only those that occur as you wrote exactly 2 files 2 times, this is because the source line is taken from file 1, and the result line written to file 2, and output the result to the screen. Please correct my code - wapdimon72ru

1 answer 1

It should not be corrected, but completely redone.

The essence of the algorithm. Choose words from the source line and add them to an intermediate table of pairs (word, number of repetitions). There may be many implementations. To start, make it simpler, for example an array of structures. For each new word, you stupidly run through all the structures, and if you find this word there, then increase the counter. If not found, then add to the end with the counter equal to 1. The size of the array (with a margin) can be estimated as the length of the input line divided by 2.

At the end, form a way out of words with a counter equal to 1.