1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
8090 [49]
3 years ago
5

Part 1: For this assignment, call it assign0 Implement the following library and driver program 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. Then write a driver program driver.c that can simply use the functions from myio library. Specifically, your driver program should get four command-line arguments: x y z output_filename. It then prompts/reads x many integers, y many doubles, and z many lines, and prints them into a file called output_filename.txt. Possible errors should be printed on stderr.
myio.h file

/*
* File: myio.h
* Version: 1.0
* -----------------------------------------------------
* 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 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. Actually, above ReadLine();
* can simply be implemented as return(ReadLineFile(stdin)); */

char *ReadLineFile(FILE *infile);

#endif
Computers and Technology
1 answer:
just olya [345]3 years ago
8 0

Answer:

Explanation:

PROGRAM

main.c

#include <fcntl.h>

#include <stdio.h>

#include <stdlib.h>

#include <sys/stat.h>

#include <sys/types.h>

#include <unistd.h>

#include "myio.h"

int checkInt(char *arg);

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

  int doubles, i, ints, lines;

  char newline;

  FILE *out;

  int x, y, z;

  newline = '\n';

  if (argc != 5) {

     printf("Usage is x y z output_filename\n");

     return 0;

  }

  if (checkInt(argv[1]) != 0)

     return 0;

  ints = atoi(argv[1]);

  if (checkInt(argv[2]) != 0)

     return 0;

  doubles = atoi(argv[2]);

  if (checkInt(argv[3]) != 0)

     return 0;

  lines = atoi(argv[3]);

  out = fopen(argv[4], "a");

  if (out == NULL) {

     perror("File could not be opened");

     return 0;

  }

  for (x = 0; x < ints; x++) {

     int n = ReadInteger();

     printf("%d\n", n);

     fprintf(out, "%d\n", n);

  }

  for (y = 0; y < doubles; y++) {

     double d = ReadDouble();

     printf("%lf\n", d);

     fprintf(out, "%lf\n", d);

  }

  for (z = 0; z < lines; z++) {

     char *l = ReadLine();

     printf("%s\n", l);

     fprintf(out, "%s\n", l);

     free(l);

  }

  fclose(out);

  return 0;

}

int checkInt(char *arg) {

  int x;

  x = 0;

  while (arg[x] != '\0') {

     if (arg[x] > '9' || arg[x] < '0') {

        printf("Improper input. x, y, and z must be ints.\n");

        return -1;

     }

     x++;

  }

  return 0;

}

myio.c

#include <limits.h>

#include <stdio.h>

#include <stdlib.h>

#include <unistd.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] == '\n' || 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\n");

     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\n");

           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\n");

     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\n");

           break;

        }

     }

  } while (go == 1);

  num = strtod(input, NULL);

  free(input);

  return num;

}

char *ReadLine(void) {

  printf("Input a line\n");

  return(ReadInput(STDIN_FILENO));

}

char *ReadLineFile(FILE *infile) {

  int fd;

  fd = fileno(infile);

  return(ReadInput(fd));

}

myio.h

#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 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. Actually, above ReadLine();

* can simply be implemented as return(ReadLineFile(stdin)); */

char *ReadLineFile(FILE *infile);

You might be interested in
Write a C++ program that consist
pshichka [43]

Answer:

Explanation:

The object-oriented paradigm; The compilation process Comments; Library inclusions; Program-level definitions; Function prototypes;

The main program; Function definitions Naming conventions; Local and global variables; The concept of a data type;

Integer types; Floating-point types; Text types; Boolean type; Simple input and

output Precedence and associativity; Mixing types in an expression; Integer division and

the remainder operator; Type casts; The assignment operator; Increment and

decrement operators; Boolean operators

7 0
3 years ago
Please help me the program c++
Ira Lisetskai [31]

 svgWrapper.style.display = 'none';

 svgWrapper.innerHTML = '<?xml version="1.0" encoding="utf-8"?><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"><symbol viewBox="0 0 32 32" style="overflow: visible" id="icon-answer" xmlns="http://www.w3.org/2000/svg"><title>answer</title><path fill-rule="evenodd" d="M28.6 0H4c-.6 0-1 .4-1 1v30c0 .6.4 1 1 1h24.6c.7 0 1-.4 1-1V1c0-.6-.3-1-1-1zM14.3 24h-4c-.7 0-1-.4-1-1s.3-1 1-1h4c.6 0 1 .4 1 1s-.4 1-1 1zm8.2-5H10.2c-.6 0-1-.4-1-1s.4-1 1-1h12.3c.6 0 1 .4 1 1s-.4 1-1 1zm0-5H10.2c-.6 0-1-.4-1-1s.4-1 1-1h12.3c.6 0 1 .4 1 1s-.4 1-1 1zm0-5H10.2c-.6 0-1-.4-1-1s.4-1 1-1h12.3c.6 0 1 .4 1 1s-.4 1-1 1z"/></symbol><symbol preserveAspectRatio="xMidYMid" viewBox="0 0 32 32" style="overflow: visible" id="icon-answered" xmlns="http://www.w3.org/2000/svg"><title>answered</title><path d="M29.064 23.266H2.936A1.936 1.936 0 0 1 1 21.329v-.002c0-1.069.867-1.936 1.936-1.936h26.128c1.069 0 1.936.867 1.936 1.936v.002c0 1.07-.867 1.937-1.936 1.937zm0-8.235H16.968a1.936 1.936 0 0 1-1.937-1.936v-.002c0-1.07.867-1.937 1.937-1.937h12.096c1.069 0 1.936.867 1.936 1.937v.002a1.936 1.936 0 0 1-1.936 1.936zm0-7.75h-6.768a1.936 1.936 0 0 1-1.937-1.936v-.002c0-1.07.867-1.937 1.937-1.937h6.768c1.069 0 1.936.867 1.936 1.937v.002a1.936 1.936 0 0 1-1.936 1.936zM9.844 14.12c-.771.981-2.078 1.241-2.918.58-.063-.049-.09-.124-.144-.18-.078-.045-.164-.063-.237-.12l-4.568-3.589A1.936 1.936 0 1 1 4.37 7.766l3.426 2.692 6.679-8.501c.771-.981 2.078-1.241 2.919-.58s.897 1.992.127 2.973l-7.677 9.77zM2.93 27.141h26.14a1.93 1.93 0 1 1 0 3.859H2.93a1.93 1.93 0 1 1 0-3.859z"/></symbol><symbol viewBox="0 0 32 32" style="overflow: visible" id="icon-arrow_double_down" xmlns="http://www.w3.org/2000/svg"><title>arrow double down</title><path d="M1.1 8.2l14 10.2c.5.4 1.2.4 1.8 0l14-10.2c.7-.5.8-1.4.3-2.1-.5-.7-1.4-.8-2.1-.3L16 15.3 2.9 5.8c-.7-.5-1.6-.4-2.1.3s-.4 1.6.3 2.1z"/><path d="M29.1 14.8L16 24.3 2.9 14.8c-.7-.5-1.6-.3-2.1.3-.5.7-.3 1.6.3 2.1l14 10.2c.5.4 1.2.4 1.8 0l14-10.2c.7-.5.8-1.4.3-2.1-.5-.7-1.4-.8-2.1-.3z"/></symbol><symbol viewBox="0 0 32 32" style="overflow: visible" id="icon-arrow_down" xmlns="http://www.w3.org/2000/svg"><title>arrow down</title><path d="M31.2 12.1c-.5-.7-1.4-.8-2.1-.3L16 21.3 2.9 11.8c-.7-.5-1.6-.3-2.1.3-.5.7-.3 1.6.3 2.1l14 10.2c.5.4 1.2.4 1.8 0l14-10.2c.7-.5.8-1.4.3-2.1z"/></symbol><symbol viewBox="0 0 32 32" style="overflow: visible" id="icon-arrow_left" xmlns="http://www.w3.org/2000/svg"><title>arrow left</title><path d="M22 31.2c.7-.5.8-1.4.3-2.1L12.8 16l9.5-13.1c.5-.7.3-1.6-.3-2.1-.7-.5-1.6-.3-2.1.3l-10.2 14c-.4.5-.4 1.2 0 1.8l10.2 14c.5.7 1.4.8 2.1.3z"/></symbol><symbol viewBox="0 0 32 32" style="overflow: visible" id="icon-arrow_right" xmlns="http://www.w3.org/2000/svg"><title>arrow right</title><path d="M10 .8c-.7.5-.8 1.4-.3 2.1L19.2 16 9.7 29.1c-.5.7-.3 1.6.3 2.1.7.5 1.6.3 2.1-.3l10.2-14c.4-.5.4-1.2 0-1.8l-10.2-14C11.6.4 10.7.3 10 .8z"/></symbol><symbol viewBox="0 0 32 32" style="overflow: visible" id="icon-arrow_up" xmlns="http://www.w3.org/2000/svg"><title>arrow up</title><path d="M.8 21.1c.5.7 1.4.8 2.1.3L16 11.9l13.1 9.5c.7.5 1.6.3 2.1-.3.5-.7.3-1.6-.3-2.1l-14-10.2c-.5-.4-1.2-.4-1.8 0L1.1 19c-.7.5-.8 1.4-.3 2.1z"/></symbol><symbol preserveAspectRatio="xMidYMid" viewBox="0 0 32 32" style="overflow: visible" id="icon-attachment" xmlns="http://www.w3.org/2000/svg"><title>attachment</title><path d="M29.333 15.747c-3.91 3.89-7.676 7.938-1

7 0
3 years ago
How do u type please help
Slav-nsk [51]

Answer:

use your keyboard

Explanation:

listen your 2 years old i know you can type daughter

6 0
2 years ago
Read 2 more answers
Encapsulate the following Python code from Section 7.5 in a function called my_sqrt that takes a as a parameter, chooses a start
Elden [556K]

Answer:

def my_sqrt(a):

   while True:

       x = a

       y = (x + a / x) / 2

       if (y == x):

           break

       else:

           x = y

   print(x)

   return 0

my_sqrt(5)

Explanation:

The above is a function in Python, and the exact copy as code of what is being given in the question. Its a method to find out the square root of a number which is a here. The while loop has been used as being mentioned in the question, and the variables are defined and calculated as being stated.

3 0
3 years ago
Identify two related tables in the JustLee Books database. Identify the common field between the two tables. Decide which column
Hatshy [7]

Answer:

Answers explained below

Explanation:

<u>The two related table are: </u>

i) Books Table

ii) BOOKAUTHOR Table

<u>The common field between the two tables are: </u>

i) ISBN attribute

<u>The columns that i would like to display are: </u>

Title, ISBN, AuthorID, PubID, PubDate, Cost, Retail, Discount, Category

<u>Sql Code to join tables using where clause </u>

select t1.Title, t1.ISBN, t2.AuthorID, t1.PubID, t1.PubDate, t1.Cost, t1.Retail, t1.Discount, t1.Category from Books t1 INNER JOIN BOOKAUTHOR t2 ON t1.ISBN = t2.ISBN where t1.ISBN = 0401140733

The above query will dispaly the attributes of table "Books" and of table "BOOKAUTHOR" for book ISBN 0401140733

<u>Repeat problem 1 but remove the WHERE statement </u>

After removing the where condition we will have following join query

select t1.Title, t1.ISBN, t2.AuthorID, t1.PubID, t1.PubDate, t1.Cost, t1.Retail, t1.Discount, t1.Category from Books t1 INNER JOIN BOOKAUTHOR t2 ON t1.ISBN = t2.ISBN

The above query will display all the mapping data of table "Books" and of Table "BOOKAUTHOR"

7 0
4 years ago
Other questions:
  • If you use the ___ template, as opposed to a formatted theme, you must make all design decisions?
    15·1 answer
  • The chief architect now needs to design a memory with an addressability of 32 bits. Suppose the architect can only use the same
    6·1 answer
  • The disk drive is a secondary storage device that stores data by _____ encoding it onto a spinning circular disk.
    14·1 answer
  • Which of the following is NOT a good idea to do after you change the root password?
    12·1 answer
  • If r is an instance of the above Person class and oddNum has been declared as a variable of type boolean, which of the following
    8·1 answer
  • Define foreign key. What is this concept used for?
    6·1 answer
  • Create class SavingsAccount. Use a static variable annualInterestRate to store the annual interest rate for all account holders.
    11·1 answer
  • A person is riding a bike and accelerating at 2.8 m/5 2 eith a force of 100 n
    6·1 answer
  • The capacity of your hand drive is measured in​
    12·1 answer
  • The int function can convert floating-point values to integers, and it performs rounding up/down as needed.
    11·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!