Can you please tell me if you have a small buffer and a stream of larger size arrives for stream input. In an infinite loop, you pass by the buffer along the line until you meet the "\ n" character, thus we recognized its length. Then I would like to consider it again in the new buffer with the appropriate size. How to return the carriage to the beginning of the line? I tried putchar ('\ r') Problem 1.16
int len; // ΠΠ»ΠΈΠ½Π° ΡΠ΅ΠΊΡΡΠ΅ΠΉ ΡΡΡΠΎΠΊΠΈ int max; // ΠΠ»ΠΈΠ½Π° ΠΌΠ°ΠΊΡΠΈΠΌΠ°Π»ΡΠ½ΠΎΠΉ ΡΡΡΠΎΠΊΠΈ ΠΈΠ· ΠΏΡΠΎΡΠΌΠΎΡΡΠ΅Π½Π½ΡΡ
int current; // ΠΠ»ΠΈΠ½Π° ΡΠ΅ΠΊΡΡΠ΅ΠΉ ΡΡΡΠΎΠΊΠΈ char line[MAXLINE]; // Π’Π΅ΠΊΡΡΠ°Ρ ΡΡΡΠΎΠΊΠ° char longest[MAXLINE]; // ΠΠ°ΠΊΡΠΈΠΌΠ°Π»ΡΠ½Π°Ρ ΠΈΠ· ΠΏΡΠ΅Π΄ΡΠ΄ΡΡΠΈΡ
ΡΡΡΠΎΠΊ max = 0; current = 0; while ((len = getline(line, MAXLINE)) > 0) { current += len; // ΠΡΠ»ΠΈ ΡΡΠΈΡΠ°Π½Π½Π°Ρ ΡΡΡΠΎΠΊΠ° Π±ΠΎΠ»ΡΡΠ΅ MAXLINE, ΡΠΎ Π²ΡΠ΄Π΅Π»ΠΈΡΡ Π½Π΅ΠΎΠ±Ρ
ΠΎΠ΄ΠΈΠΌΡΠΉ ΡΠ°Π·ΠΌΠ΅Ρ if (line[len - 1] == '\n') { putchar('\r'); getline(line, current); max = current; copy(longest, line); } } if (max > 0) //ΠΡΠ»Π° Π»ΠΈ Ρ
ΠΎΡΡ ΠΎΠ΄Π½Π° ΡΡΡΠΎΠΊΠ°? printf("%s \n", longest); //ΠΠ΅ΡΠ°ΡΡ ΡΠ°ΠΌΠΎΠΉ Π΄Π»ΠΈΠ½Π½ΠΎΠΉ ΡΡΡΠΎΠΊΠΈ return 0; int getline(char s[], int lim) { int c, i; for (i = 0; i < lim - 1 && (c = getchar()) != EOF && c != '\n'; ++i) s[i] = c; if (c == '\n') { s[i] = c; ++i; } s[i] = '\0'; return i; } // copy - ΠΊΠΎΠΏΠΈΡΡΠ΅Ρ ΡΡΡΠΎΠΊΡ ΠΈΠ· 'from' Π² 'to', 'to' - Π΄ΠΎΡΡΠ°ΡΠΎΡΠ½ΠΎ Π±ΠΎΠ»ΡΡΠΎΠΉ void copy(char to[], char from[]) { int i = 0; while ((to[i] = from[i]) != '\0') ++i; }