Question: Write a program entab that replaces strings of blanks by the minimum number of tabs and blanks to achieve the same spacing. Use the same

Write a program entab that replaces strings of blanks by the minimum number of tabs and blanks to achieve the same spacing. Use the same tab stops as for detab. When either a tab or a single blank would suffice to reach a tab stop, which should be given preference?

Our teacher the code is resuable so we are assuming we have to change the main around to be able to make the problem work which is 1.21.c

what are you asking specifically? the other questions i asked similar to this was answered

//1.21.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.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 */ //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 }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!