user11473512
user11473512

Reputation:

C programming read multiple lines and pause until pressing any key from keyboard

I am very new in C programming. I hope I can explain my problem. I am trying to develop a program to read a binary file and convert to ASCII mode. There was no any problem for this. But what I have to do is ask user ones, how many lines he/she wants to read(for example 20 lines) then only show 20 lines from binary file and ask user press any key to continue. After pressing any key then again show next 20 lines from that file and so on.

I tried to use getch() and getchar() but that worked only for one line.

My following code may help to explain properly. Please help me out from this. Thank you in advance.

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

void header(); //Title function declaration

int main(int argc, char *argv[])
{
    FILE *fp; // File pointer declaration
    int i, j;
    unsigned char buffer[17]; // Trying to get value for 16 bites each

    fp = fopen(argv[1], "rb"); 
    if (argc == 1) { // Condition for if user didnt specify file name
        /* Assume that argv[0] is the program name */
        printf("usage: %s, <file_name>", argv[0]);
        return 0;
    }

    if (fp == NULL) { // condition if there is no file 
        fprintf(stderr, "File Open error (%s) , error = %d", argv[1], errno);
        exit(1);
    }

    else
    {
        header(); // Calling function header

        int read = 0;
        int address = 0;
        while ((read = fread(buffer, 1, 16, fp)) > 0)
        {
            printf("%08X", address); 
            printf(" ");
            address += 16;

            for (i = 0; i < read; i++)
            {
                if (i == 8) {
                    if (buffer[i] != NULL)
                        printf("-");
                }
                else {

                    printf(" ");
                }
                printf("%02X", buffer[i]);
            }

            int space = 16 - read;

            if (space != 0) {

                for (int x = 0; x < space; x++) {
                    int y = read + (x - buffer[x]);
                    printf("   ");
                }
            }
            printf("  ");
            for (j = 0; j < read; j++)
            {
                if (buffer[j] == NULL) {
                    printf(".");
                }
                if (isprint(buffer[j])) {
                    printf("%c", buffer[j]);
                    //fflush(stdin);
                }
                else {
                    if (buffer[j] != NULL) {
                        printf(".");
                    }
                }
            }
            printf("\n");

        }

    }
    fclose(fp);

    return 0;
}

void header() {
    printf("ADDRESS  ");

    for (int i = 0X00; i <= 0X0F; i++)
    {
        if (i == 8) {
            printf("-");
        }
        else
        {
            printf(" ");
        }
        printf("%02X", i);
    }
    printf("  0123456789ABCDEF \n");
    printf("----------------------------------------------------------------------------\n");

}

And output should be like Sample

Upvotes: 1

Views: 351

Answers (1)

kiran Biradar
kiran Biradar

Reputation: 12732

You need to surround your reading and printing logic inside loops.

int read = 0;
int address = 0;
int numLines = 0;

printf("How many lines you want to print?");
scanf("%d", &numLines);
do {

       for(int i = 0; i<numLines; i++)
       {
                if ((read = fread(buffer, 1, 16, fp)) > 0)
                {
                      ....... //your printing logic here
                }
       }

       /*Flush input stream in case \n left out*/
       int c;
       while ((c = getchar()) != '\n' && c != EOF) { }

       printf("press Any key to continue\n");
       getchar();
 } while(read>0);

Upvotes: 1

Related Questions