0

I have a program that has a text file that is variable in length. It must be capable of being printed in the terminal. My problem is that if the code is too large, part of it becomes inaccessible due to the limited scroll of terminal. I was thinking of having a command executed by a character to continue the lines after a certain point, allowing the user to see what they needed, and scroll if they needed. However the closest I have come is what you see here, which prints the text file one line at a time as you press enter. This is extremely slow and cumbersome. Is there another 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 is an error message!");
            return 13;
        }
   }
   fclose(audit);
   return 0;
}

The libraries are included as I tried various methods. Perhaps there is something obvious I am missing, however after looking around I found nothing that suited my needs in C.

Santosh A
  • 5,173
  • 27
  • 37
Zach Ross
  • 69
  • 1
  • 8

2 Answers2

1

You can keep a count of something like num_of_lines and keep incrementing it and when it reaches some number(say 20 lines) then do a getchar() instead of doing it for each line.

Make sure you don't use feof() as already suggested. Just for the purpose of how it can be done I am showing the 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 the same thing in your 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 is an error message!");
    return 13;
    }

    num_of_lines++; 
    if(num_of_lines == 20)
    {
      num_of_lines = 0;
      getch();
    } 
   }
   fclose(audit);
   return 0;
}
Gopi
  • 19,784
  • 4
  • 24
  • 36
  • I like it, but how? Do I set up a character count or something? @Gopi – Zach Ross Nov 27 '14 at 07:50
  • is there anyway you can reformat that, I am not formatting it right in my code... @Gopi – Zach Ross Nov 27 '14 at 08:08
  • @JamesZ Sorry I didn't get what you want? Reformat what? Don't use `feof()` instead use `fgets()` itself – Gopi Nov 27 '14 at 08:09
  • I keep putting what you wrote in my program wrong, any way you can show how that works a little better? – Zach Ross Nov 27 '14 at 08:13
  • @JamesZ Check the latest edits. Your changes to read the file is wrong – Gopi Nov 27 '14 at 08:16
  • I added that, and it looks great. With some tailoring, I can get it to be what I need :) I already edited out `feof()` for `fgets()` in the question. – Zach Ross Nov 27 '14 at 08:28
  • If this has answered your question then mark as answered right way to thank on stack overflow – Gopi Nov 27 '14 at 08:29
0

From the man page of fgets()

fgets() reads in at most one less than size characters from stream and stores them into the buffer pointed to by s.
Reading stops after an EOF or a newline. If a newline is read, it is stored into the buffer. A terminating null byte is stored after the last character in the buffer.

So char length_of_code[60000]; is not a better option.

Try to set the size of array to optimum value which in most case is 80.

Also as fgets fetches line by line you will have to output line by line untill EOF

EDIT:

1. 2nd argument to fprintf should be the format specifier and not length
2. 3rd arg should be a string and not the file pointer

fprintf(stdout, "%s", length_of_code);

Code Snippet:

 while (fgets(length_of_code, sizeof(length_of_code), audit))
 {
    fprintf(stdout, "%s",  length_of_code);
    getch();

    if (ferror(audit))
    {
        printf("This is an error message!");
        return 13;
    }
 }
Santosh A
  • 5,173
  • 27
  • 37
  • I was trying to use fread() before, however I couldn't get it to do what I needed, this however got me closer... @Santosh – Zach Ross Nov 27 '14 at 07:58
  • If so, why can I not get it to run when i replace 'length_of_code' with '%s'? @Santosh – Zach Ross Nov 27 '14 at 08:06
  • @JamesZ with the added code snippet you will be able to print one line at a time. From here you can improvise to print specific number of lines – Santosh A Nov 27 '14 at 09:13