Wednesday 9 October 2013

Display A File With Breaks/Pauses

Some real life obligations have been keeping me too busy to study more C programming. I knew this was coming.....lol.

Anyway, this program displays a file. Every 20 lines, it waits for a key press. To change the number of lines it pauses at, change the 20 in
if(lines%20==0 && lines!=0)
to any number.
Also, I used a file called "test.txt" ( fp=fopen("test.txt","r");). It may be changed to any file name.
Eg:
 fp=fopen("C:\\Users\\YOURUSERNAME\\desktop\\SomeDocument.txt","r");

Here's the program:

#include <stdio.h>
#include <conio.h>

int main()

{
    FILE *fp;
    char c;
    int lines=0;

    fp=fopen("test.txt","r");

    for(;;)
    {
        c=fgetc(fp);
        if(c==EOF)
        {
            break;
        }
        else if(c=='\n')
        {
            lines++;
        }

        if(lines%20==0 && lines!=0)

        {
            printf("%c",c);
            getch();
            lines++;
        }
        else
        {
            printf("%c",c);
        }
    }
    return 0;
}

No comments:

Post a Comment