Reading a text file in C, stopping at multiple points, breaking it into sections -


i have program has text file variable in length. must capable of being printed in terminal. problem if code large, part of becomes inaccessible due limited scroll of terminal. thinking of having command executed character continue lines after point, allowing user see needed, , scroll if needed. closest have come see here, prints text file 1 line @ time press enter. extremely slow , cumbersome. there solution?

#include <stdio.h> #include <stdlib.h> #include <string.h> #include <errno.h>   int main() { file *audit; audit = fopen("checkout_audit.txt", "r"); char length_of_code[60000]; int ch;      while ((ch = fgetc(audit)) != eof)     {         fgets(length_of_code, sizeof length_of_code, audit);         fprintf(stdout, length_of_code, audit);         getch();          if (ferror(audit))         {             printf("this error message!");             return 13;         }    }    fclose(audit);    return 0; } 

the libraries included tried various methods. perhaps there obvious missing, after looking around found nothing suited needs in c.

you can keep count of num_of_lines , keep incrementing , when reaches number(say 20 lines) getchar() instead of doing each line.

make sure don't use feof() suggested. purpose of how can done showing below snippet.

int num_of_lines = 0;  while(!feof(fp)) { // fgets();  num_of_lines++;  if(num_of_lines == 20) { num_of_lines = 0; getch(); } } 

putting same thing in code:

int main() { file *audit; audit = fopen("checkout_audit.txt", "r"); char length_of_code[60000]; int num_of_lines = 0; int ch;      while (fgets(length_of_code, sizeof length_of_code, audit) != null)    {     fprintf(stdout, length_of_code, audit);     if (ferror(audit))     {     printf("this error message!");     return 13;     }      num_of_lines++;      if(num_of_lines == 20)     {       num_of_lines = 0;       getch();     }     }    fclose(audit);    return 0; } 

Comments

Popular posts from this blog

matlab - "Contour not rendered for non-finite ZData" -

delphi - Indy UDP Read Contents of Adata -

javascript - Any ideas when Firefox is likely to implement lengthAdjust and textLength? -