Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I need help writing the file driver2.c , I have provided myio.c and myio.h: Implement the following library and driver programs under assign0: Your library

I need help writing the file driver2.c , I have provided myio.c and myio.h:

Implement the following library and driver programs under assign0: Your library will be consisting of myio.h and myio.c. The function prototypes as well as more explanations are listed in myio.h. Please download it and accordingly implement the exported functions in myio.c. Basically, you are asked to develop a simple I/O library which exports a few functions to simplify the reading of an integer, a double, and more importantly a string (whole line). In contrast to standard I/O functions that can read strings (e.g., scanf with "%s", fgets) into a given static size buffer, your function should read the given input line of characters terminated by a newline character into a dynamically allocated and resized buffer based on the length of the given input line. Also your functions should check for possible errors (e.g., not an integer, not a double, illigal input, no memory etc.) and appropriately handle them.

Second one (say driver2.c) gets two command-line arguments: input_file.txt output_file.txt. Here is a sample input_file.txt , which is a text file containig many lines. Your program reads each line and removes theextra space characters between the words and prints the new line into output_file.txt. So there will be at most one space character between the words in output_file.txt

myio.h:

/* * File: myio.h * Version: 1.0 * Last modified on Wed Dec 12 11:37:26 2016 by korkmaz * This is mostly from a similar file provided by eroberts * ----------------------------------------------------- * This interface provides access to a basic library of * functions that simplify the reading of input data. */ #ifndef _myio_h #define _myio_h /* * Function: ReadInteger * Usage: i = ReadInteger(); * ------------------------ * ReadInteger reads a line of text from standard input and scans * it as an integer. The integer value is returned. If an * integer cannot be scanned or if more characters follow the * number, the user is given a chance to retry. */ int ReadInteger(void); /* * Function: ReadDouble * Usage: x = ReadDouble(); * --------------------- * ReadDouble reads a line of text from standard input and scans * it as a double. If the number cannot be scanned or if extra * characters follow after the number ends, the user is given * a chance to reenter the value. */ double ReadDouble(void); /* * Function: ReadLine * Usage: s = ReadLine(); * --------------------- * ReadLine reads a line of text from standard input and returns * the line as a string. The newline character that terminates * the input is not stored as part of the string. */ char *ReadLine(void); /* * Function: ReadLine * Usage: s = ReadLine(infile); * ---------------------------- * ReadLineFile reads a line of text from the input file which * is already open and pointed by infile. It then reads the line, * dynamically allocates space, and returns the line as a string. * The newline character that terminates the input is not stored * as part of the * string. * The ReadLine function returns NULL if infile is at the * end-of-file position. * Note: the above ReadLine(); can simply be implemented as * { return(ReadLineFile(stdin)); } */ char *ReadLineFile(FILE *infile); #endif 

myio.c:

/* * File: myio.c * Version: 1.0 * Created on: Sept 6, 2017 * Author: Santiago Villasenor */ #include #include #include #include #include #include #include #include "myio.h"

char *ReadInput(int fd) { char buf[BUFSIZ]; int i; char *input; int r, ret, x;

i = 1; r = 0; ret = 1;

input = calloc(BUFSIZ, sizeof(char));

while (ret > 0) { ret = read(fd, &buf, BUFSIZ); for (x = 0; x < BUFSIZ; x++) { if (buf[x] == ' ' || buf[x] == EOF) { ret = -1; break; } input[x*i] = buf[x]; r++; } i++; if (ret != -1) input = realloc(input, BUFSIZ*i); }

if (r == 0) return NULL;

input[r] = '\0'; input = realloc(input, r+1);

return(input); }

int ReadInteger() { char *input; int go, num, x; go = 0;

do { go = 0; printf("Input an integer "); input = ReadInput(STDIN_FILENO); for (x = 0; x < INT_MAX; x++) { if (x == 0 && input[x] == '-') continue; if (input[x] == 0) break; else if (input[x] > '9' || input[x] < '0') { go = 1; printf("Improper input "); break; } } } while (go == 1);

num = atoi(input); free(input);

return num; }

double ReadDouble(void) { int dec, exp; char *input; int go; double num; int x;

do { go = 0; dec = 0; exp = 0; printf("Input a double "); input = ReadInput(STDIN_FILENO); for (x = 0; x < INT_MAX; x++) { if (x == 0 && input[x] == '-') continue; if (input[x] == 0) break; else if (input[x] == '.' && dec == 0) dec = 1; else if (x != 0 && (input[x] == 'e' || input[x] == 'E') && exp == 0) { dec = 1; exp = 1; } else if (input[x] > '9' || input[x] < '0') { go = 1; printf("Improper input "); break; } } } while (go == 1);

num = strtod(input, NULL); free(input);

return num; }

char *ReadLine(void) { printf("Input a line "); return(ReadInput(STDIN_FILENO)); }

char *ReadLineFile(FILE *infile) { int fd;

fd = fileno(infile);

return(ReadInput(fd)); }

input_file.txt:

This line has only one space between the words. This line has so many spaces between words. We want the second line and this line to be formated as the first line. 

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

New Principles Of Best Practice In Clinical Audit

Authors: Robin Burgess

2nd Edition

1138443646, 978-1138443648

More Books

Students also viewed these Accounting questions

Question

L A -r- P[N]

Answered: 1 week ago

Question

Identify conflict triggers in yourself and others

Answered: 1 week ago