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; } 
  • and add the code with the input - Grundy
  • What does it mean to "reread it"? Data received from stdin cannot be rewound. Therefore, yes, I would like to see exactly how they are introduced. - PinkTux
  • By the way, if we are talking about task 1.16, then in the original it does not sound the way indicated in the question. Either someone did not understand the condition, or is it just a curved translation. - PinkTux
  • @PinkTux read getchar () from the console. Those. Is it possible to return to the beginning of the stream after it was considered? The task is: print the longest line without any restrictions on its size! - ActiveNum

2 answers 2

In your formulation without dynamic memory allocation, the problem is not solved in principle. But the fact is that in the original the problem is formulated differently. Compare, Russian translation:

Exercise 1.16. Rewrite the main program of the previous program so that it can print the longest line without any restrictions on its size.

And the original:

Exercise 1-16. It is a good idea to make a note of the text.

And in this formulation, it is already solved without problems.

Conclusion: always check the input data for correctness, and not only in the program code :-)

  • Thank! As I understand it is necessary to find out the length of the longest string, regardless of the length of the buffer. Right? - ActiveNum pm
  • Yes. And to bring out the maximum of what can be extracted from it (that is, if you fit all the way - all, if not fit, how much you will). - PinkTux
  • @ActiveNum yes, if the readable line does not fit into the buffer, then divide it into 2 parts and start reading in the second half (dynamically shifting its beginning to the middle of the buffer) so that in the end something like that "это Π½Π°Ρ‡Π°Π»ΠΎ строки Π° Π΅Π΅ сСрСдина Π½Π΅ ΠΏΠΎΠΌ...ΠΈ Π²ΠΎΡ‚ ΠΊΠΎΠ½Π΅Ρ† нашСй Π΄Π»ΠΈΠ½Π½ΠΎΠΉ строки\n" - "это Π½Π°Ρ‡Π°Π»ΠΎ строки Π° Π΅Π΅ сСрСдина Π½Π΅ ΠΏΠΎΠΌ...ΠΈ Π²ΠΎΡ‚ ΠΊΠΎΠ½Π΅Ρ† нашСй Π΄Π»ΠΈΠ½Π½ΠΎΠΉ строки\n" (the program inserts 3 points in the middle) / Try your hand. - avp

Isn't it easier to do a realloc buffer with some sort of reasonable strategy, for example, doubling it?

  • malloc is not used at all, limited by the condition of the problem! char buf [BUF_SIZE], so memory is allocated - ActiveNum