Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Write a program detab that replaces tabs in the input with the proper number of blanks to space to the next tab stop. Assume a

Write a program detab that replaces tabs in the input with the proper number of blanks to space to the next tab stop. Assume a fixed set of tab stops, say every n columns. Should n be a variable or a symbolic parameter? the answer is technically given which we cant seem to find out why it does not work specifically.

//main.c

#include "files.h" #include #include #include

#define TAB_INTERVAL 10 #define MAX_TAB_INTERVAL 80

int main(int argc, const char *argv[]) { FILE *fin; FILE *fout;

// set tab_interval or use default int tab_interval = argc == 4 ? atoi(argv[3]) : TAB_INTERVAL; if (tab_interval > 80) { fprintf(stderr, "tabstops must be <= 80 characters "); exit(2); }

// open input and output files if (!open_io_files( argc, argv, &fin, &fout, 3, 4, "Usage: ./detab inputfile outputfile tab_interval (optional) ")) { exit(1); }

// process input file to remove tabs and substitute spaces int c; char maxspaces[MAX_TAB_INTERVAL + 1]; char spaces[MAX_TAB_INTERVAL + 1]; memset(maxspaces, ' ', sizeof(maxspaces));

int bytes_written = 0; while ((c = fgetc(fin)) != EOF) { if (c != '\t') { fputc(c, fout); ++bytes_written; } else { int bytes_to_tab = tab_interval - (bytes_written % tab_interval); strncpy(spaces, maxspaces, bytes_to_tab); spaces[bytes_to_tab] = '\0'; fputs(spaces, fout); bytes_written += strlen(spaces); } } closefiles(2, fin, fout); // must say number of files

return 0; } //files.c

// // files.c //

#include "files.h"

bool open_io_files(int argc, const char *argv[], FILE **fin, FILE **fout, int min_expected_argc, int max_expected_argc, const char *usage) { /* open an input file, and optionally an output file */ if (argc < min_expected_argc || argc > max_expected_argc) { fprintf(stderr, "%s ", usage); return false; }

*fin = fopen(argv[1], "r"); if (*fin == NULL) { fprintf(stderr, "failed to open input file: '%s' ", argv[1]); return false; } // In this case, we don't want to open output file if (fout == NULL) { return true; } // everything cool

*fout = fopen(argv[2], "w"); if (*fout == NULL) { // output file failed to open fprintf(stderr, "failed to open output file: '%s' ", argv[2]); fprintf(stderr, "closing already open input file: '%s' ", argv[1]); fclose(*fin); return false; }

return true; }

// must include -- see files.h void closefiles(int n, ...) { // uses varargs (variable # of args) va_list pargs; va_start(pargs, n); // initialize the list ptr

for (int i = 0; i < n; i++) fclose(va_arg(pargs, FILE *)); // get next argument

va_end(pargs); // clean up }

files.h

// // files.h // detab // // Created by William McCarthy on 032//20. // Copyright 2020 William McCarthy. All rights reserved. //

#ifndef files_h #define files_h

#include #include #include

bool open_io_files(int argc, const char* argv[], FILE** fin, FILE** fout, int min_expected_argc, int max_expected_argc, const char* usage);

void closefiles(int n, ...);

#endif /* files_h */

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

Database Concepts

Authors: David Kroenke

4th Edition

0136086535, 9780136086536

More Books

Students also viewed these Databases questions

Question

Briefly explain the various types of leadership ?

Answered: 1 week ago

Question

Explain the need for and importance of co-ordination?

Answered: 1 week ago

Question

Explain the contribution of Peter F. Drucker to Management .

Answered: 1 week ago

Question

What is meant by organisational theory ?

Answered: 1 week ago

Question

Did the team members feel that their work mattered

Answered: 1 week ago

Question

3. What may be the goal of the team?

Answered: 1 week ago