I need help writing the file driver2.c , I have provided myio.c and myio.h: Seco
ID: 3876726 • Letter: I
Question
I need help writing the file driver2.c , I have provided myio.c and myio.h:
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:
myio.c:
/*
* File: myio.c
*/
#include<ctype.h>
#include <limits.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<sys/types.h>
#include<unistd.h>
#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] = '';
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:
Explanation / Answer
Given below is the code for driver2.c. While implementing it, I noticed that the function ReadLine(FILE *fp) did not function as desired. It returned mulitple lines (to fit BUFSIZE length) , but the file myio.h clearly documents that only 1 line should be returned with the newline removed. So I have modified the function ReadLine(FILE *fp) to do exactly as written in its comments in myio.h. I am not sure if you are using ReadInput(int fd) , the version with filedescriptor. So left the function as is. If you don'd really need the ReadInput(int fd), (I don't see its declaration in myio.h) , please remove that function from myio.c. ReadLine(FILE *fp) does not use it.
Check out the modified/ new files. Hope the answer helps. If it did, please don't forget to rate the answer. Thank you very much.
myio.c (modified)
/*
* File: myio.c
*/
#include<ctype.h>
#include <limits.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<sys/types.h>
#include<unistd.h>
#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] = '';
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) {
char *input = calloc(BUFSIZ, sizeof(char));
int x;
if(fgets(input, BUFSIZ, infile))
{
for(x = 0; x < BUFSIZ && input[x] != ''; x++)
if(input[x] == ' ')
break;
input[x] = '';
return input;
}
else
{
return NULL;
}
}
driver.c (new )
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "myio.h"
int main(int argc, char *argv[])
{
FILE *infile, *outfile;
char *inputLine, *outputLine;
int idx1 , idx2;
int stop;
char ch;
if(argc != 3) //check for correct number of arguments
{
printf("usage: %s <input_file> <ouput_file> ", argv[0]);
return 1;
}
//open the files
infile = fopen(argv[1], "r");
if(infile == NULL)
{
printf("input file %s could not be opened ", argv[1]);
return 1;
}
outfile = fopen (argv[2], "w");
if(outfile == NULL)
{
printf("output file %s could not be opened ", argv[2]);
return 1;
}
while((inputLine = ReadLineFile(infile)) != NULL)
{
outputLine = calloc(strlen(inputLine), sizeof(char));
idx1 = 0;
idx2 = 0;
stop = 0;
while(!stop)
{
ch = inputLine[idx1++];
//copy all non-space characters
while(ch != ' ' && ch != ' ')
{
if(ch == '')
{
stop = 1;
break;
}
outputLine[idx2++] = ch;
ch = inputLine[idx1++];
}
if(!stop)
{
//put a single space in output line
outputLine[idx2++] = ' ';
//skip over all white space character
while(inputLine[idx1] == ' ' || inputLine[idx1] == ' ')
idx1++;
}
else
{
outputLine[idx2] = '';
fprintf(outfile, "%s ", outputLine);
}
}
//free all allocated space
free(inputLine);
free(outputLine);
}
fclose(infile);
fclose(outfile);
printf("Generated output file %s ", argv[2]);
}
output
$ gcc myio.c driver2.c
$ ./a.out
usage: ./a.out <input_file> <ouput_file>
$ ./a.out input_file.txt output_file.txt
Generated output file output_file.txt
output file: output_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.