#include <stdio.h> #define N 255 void main() { char mem[N]; FILE * fil; int temp, k, length = 60; fil = fopen("D:\\FilesProjects\\1file.txt", "rt"); while (fgets(mem,N,fil)!=NULL) { if (strlen(mem) >= 60) printf("%s \n", mem); } fclose(fil); system("pause"); } 

Is it possible to remake this code so that it displays lines from a file of a certain length WITHOUT using arrays or strings? Really needed.

    2 answers 2

    Yes you can. You need to be able to find and memorize line boundaries, where the left border is the beginning of the file or <position of the last line feed + 1>, and the right line is the position of the next line feed. That is, you remember the position of the beginning of the line, read the characters and count their number with getchar() until you meet the newline or the end of the file, then, if the number of read characters matches the condition for outputting the line, use the fseek to move the cursor read the file to the previously remembered position of the beginning of the line, read the characters through getchar() again and immediately output them to the end of the line. Repeat for each line, not forgetting to reset the counter of counted characters in the line.

      Maybe so?

       long cur = 0; while (fgets(mem,N,fil) !=NULL) { long next = fseek(fil, 0, SEEK_CUR); if ((next - cur) > 60) fputs(mem,fil); cur = next; } fputc('\n',fil); 
      • Thank you, but I already did - Kirill Himov