Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

C CODE PLEASE USE CORRECT MEMORY ALLOCATION AND REALLOC Thanks in advance 1. Description Write a C program that will interactively display text file contents

C CODE PLEASE USE CORRECT MEMORY ALLOCATION AND REALLOC Thanks in advance

image text in transcribedimage text in transcribedimage text in transcribed

1. Description Write a C program that will interactively display text file contents in either ASCIl or hexadecimal (hex) mode. It will essentially be a combination of the Unix command line utilities cat and hexdump (in simplified form), but with a basic user menu. Use this as a reference for the ASCII table: https://www.asciitable.com/ Note that this assignment uses the standard C notation for hexadecimal numbers. For example, 09 is the hex number with the decimal value 9 , and 01F - hex number with the decimal value 31. ASCII mode In ASCII mode, the program will simply display the contents. However, not all ASCll characters are visible - some are (or used to be) used for various control sequences - so your program must replace them with a blank space in the displayed output. Specifically, do the following replacements when displaying the file contents: - replace the characters in the range 00 - 09,0B - 01F with space (ASCll code 020) - replace the characters in the range 07F and greater with the character ? (ASCII code 03F ) Since you will not be replacing 0xA (Line Feed, or LF), the newlines will be displayed as usual, and the file should look the same as if you display it with the cat command. HEX mode In hex mode, your output will match how hd (hexdump C) displays the file contents, 16 characters at a time, though we will make a simplifying change: - The first 7 chars will display the character index in hex - 0000000 for the first line - i.e. first 16 characters - 0000010 for the second line - i.e. the next set of up to 16 characters. Since we're starting with character number 16(010), the line number is 0000010. - The characters will be displayed one at a time, with a space after each character, and two spaces after the first eight characters. Unlike hd, you do not need to display the ASCII version of these characters incased in ||. - The very last line is always the index of last character+1 - e.g. 0x1f, or 31 in the example below. For example, given a sample file stuff. txt in the assignment description, the output of your program should be: ASCll mode: 1234567789 ABCDEFGhijklmn 11111 Program execution Your program will run interactively as follows. It will start in ASCIl mode by default and will prompt the user with a main menu that displays the following options: - Clearly display the current output mode (ASCII or hex) and asking the user to press - "o" to enter a file name, - "d" to select display mode - "x" exit - If the user presses "d", ask them to enter the mode - "a" for ASCII, "h" for hex - and display the main menu - If the user presses "o", ask for the file name, read it in, then try to open it and do the following: - Read the entire file into a memory using the Unix open and read system calls. The buffer must be dynamically allocated with malloc. - If the file cannot be opened, print "cannot open file X " (X being the file name) and display the main menu - Display file contents according to the current mode, as described above. - After displaying the contents ask the user for input: - "m" to return to the main menu. Remember to close the file descriptor. - "x" to exit - In either menu, if the user presses " X ", exit the program. Make sure all file descriptors are closed and all memory has been freed when you exit. - Make sure you always check for invalid input. If the user enters an invalid key in any menu, display "invalid input" and re-display the menu. 2. Evaluation Your code must compile, run, and have all of the specified functionality implemented. Any compiler errors will result in the automatic grade of zero for the assignment. 2 Your code will be tested with several text files. Marks will be deducted for: - Incorrect and missing functionality - Deviations from the assignment requirements - File descriptions that you opened yourself and that are still open when your program terminates - valgrind will be used for checking this - Run-time errors, including infinite loops, crashes, etc. - Compiler warnings - Failure to follow submission instructions For A1, you will not be penalized for memory leaks and other memory errors reported by valgrind. However, future assignments will include such penalties. It is strongly recommended that you test your A1 using valgrind as you develop it, so you learn how to use valgrind and understand its output

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Databases Organizing Information Digital And Information Literacy

Authors: Greg Roza

1st Edition

1448805929, 978-1448805921

More Books

Students also viewed these Databases questions

Question

Identify three types of physicians and their roles in health care.

Answered: 1 week ago

Question

Compare the types of managed care organizations (MCOs).

Answered: 1 week ago