QuestionQuestion

Transcribed TextTranscribed Text

Your job is to write the program data_uncompress, using the C standard I/O library. In particular, you should read using getchar() and fread(). This program assumes that standard input is in a compressed format. The format is a stream of bytes as follows. You read a character, and that character tells you how to proceed: If the character is not 'n', 's', 'i' or 'd', then the input is not correct. If the character is n', then print a newline. If the character is 'i', d' or 's', then the next character needs to be interpreted as an integer (unsigned char). Call this integer n. Then you are going to read n+1 contiguous integers, doubles or strings, depending on whether the initial character was 'i', d' or 's'. Integers are 4 bytes and doubles are 8 bytes. Strings are in a special format. You read a character size which you interpret as an integer (again -- unsigned char). The string is in the remaining size+1 characters. Obviously, since size and n are unsigned chars, their values will be limited to 255 or less. You are going to print each integer, double or string on the same line, separated by a single space. You only print a newline when you see the'n'. Do not start a new line with a space. Simply print integers with "%d". Print doubles with "%. 10lg" (that's percent-dot-one-zero-ell-gee). Continue reading in this vein until you reach EOF. You only print a newline when you see the "n character. Here's an example file in comp-1.txt -- each cell of the table is a character of the file: 115 ('s') 1 2 74 ('J') 105 ('i') 109 ('m') 4 80 ('P') 108 (')') 97 ('a') 110('n') 107 ('k') 110('n') 115(s') 4 ('C') 83 (''') (33) 54 (66) 48 ('0') 110(n') When you uncompress this, you first see the 's', and then a one. That means you need to read two strings. The first consists of three characters, which are 'J', 'i' and 'm', and the second consists of five characters "Plank". You print "Jim", a space and "Plank." Then you read a 'n', and print a newline. Next you read a's' and the number 0, which means to read one string. That string has five characters: "CS360". You print "CS360", then read the final "n and print a newline. Thus: UNIX> data uncompress < comp-1. txt Then comp-2.txt is composed of the following bytes: 105(i) 210 4 100('d') 110 134 27 240 249 33 9 64 115('s') 3 70(F) 114(r') 101(e') 100(d') 110(n') To uncompress, you first read the 'i' and the 0, which says to read one integer. You will read the next four bytes (210, 4, 0, 0) using fread(). You next read the byte 'd' and a 0, which say to read one double. You will thus read the next eight bytes (110, 134,27, 40, 249, 33, 9, 64) again using fread(). Next you read an 's' and 0, which says to read one string. You read 3, which says that the string is four characters (Fred). After reading those, you read the 'n' which says to print a newline. The output is thus: UNIX> data uncompress < comp-2 txt 1234 3.14159 Fred UNIX> There are additional test files in the test directory. To test your program, you can use the diff command. UNIX> data uncompress < t003.txt > my_003.txt UNIX> diff -y my 003.Ext 003-uncompressed.txt UNIX> A Common Mistake When you read n and size, you read them as unsigned chars. However, when you use them in your computer program, you should convert them to ints. Why? Because if you read in 255 as an unsigned char, and then add one to it, it becomes zero, and that is not what you want, is it? Error catching Your program must catch the following errors, and then print the given strings on standard error: If the type character is not'n', 's','i' or 'd', then print "Input error: bad type\n" If you don't get the size byte n, print "Input error: no size\n" If you don't get the string size byte size, print "Input error: no string size\n" If you don't get enough ints, print "Input error: not enough ints\n" If you don't get enough doubles, print "Input error: not enough doubles\n" If you don't get enough characters in a string, print "Input error: not enough chars\n

Solution PreviewSolution Preview

This material may consist of step-by-step explanations on how to solve a problem or examples of proper writing, including the use of citations, references, bibliographies, and formatting. This material is made available for the sole purpose of studying and learning - misuse is strictly forbidden.

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

int main(int argc, char** argv) {

    char c;
    unsigned char size;
    unsigned char n;
    int i;
    char buf[200];
    int tmp1;
    int tmp2;
    double tmp3;
    c = getchar();   
    while (c != EOF) {
      
       switch(c){
            case 'n':...
$20.00 for this solution

PayPal, G Pay, ApplePay, Amazon Pay, and all major credit cards accepted.

Find A Tutor

View available C-Family Programming Tutors

Get College Homework Help.

Are you sure you don't want to upload any files?

Fast tutor response requires as much info as possible.

Decision:
Upload a file
Continue without uploading

SUBMIT YOUR HOMEWORK
We couldn't find that subject.
Please select the best match from the list below.

We'll send you an email right away. If it's not in your inbox, check your spam folder.

  • 1
  • 2
  • 3
Live Chats