C Language and Linux Create a program to read the byte conte

C Language and Linux

Create a program to read the byte contents of any file and print them out.

The program will take between 1 and 3 command-line arguments: a manditory lename for the input, optionally the number of bytes to print, and optionally the starting position in the le. Both numbers are in decimal. If the 3rd argument is not given, the starting position is byte 0 [beginning of the le; if the second argument is not given, print 64 [0x40] bytes.

The output goes to standard output. Each line starts with the byte position of the rst byte in hexadecimal followed by a tab. This is followed by 16 bytes of the le printed as hexadecimal numbers, blank separated. Finaly a blank and the same 16 bytes printed as ascii characters if between 0x20 [a space] and 0x7e [’~’], and as a dot if outside that range.

Solution

#include <stdio.h>

#include <stdlib.h> // For exit()

int main()

{

    FILE *fptr;

    char filename[100], c;

    printf(\"Enter the filename to open \ \");

    scanf(\"%s\", filename);

    // Open file

    fptr = fopen(filename, \"r\");

    if (fptr == NULL)

    {

        printf(\"Cannot open file \ \");

        exit(0);

    }

    // Read contents from file

    c = fgetc(fptr);

    while (c != EOF)

    {

        printf (\"%c\", c);

        c = fgetc(fptr);

    }

    fclose(fptr);

    return 0;

}

C Language and Linux Create a program to read the byte contents of any file and print them out. The program will take between 1 and 3 command-line arguments: a

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site