Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Write a program to fold long input lines into two or more shorter lines after the last non-blank character that occurs before the n-th column

Write a program to fold long input lines into two or more shorter lines after the last non-blank character that occurs before the n-th column of input. Make sure your program does something intelligent with very long lines, and if there are no blanks or tabs before the specified column.

our teacher says we have reusable code so this is the code that was given; and that we have to edit main around only which is 1.22.c

be specific what is needed in here idk what ,more info is needed

//1.22.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

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

Understanding Oracle APEX 5 Application Development

Authors: Edward Sciore

2nd Edition

1484209893, 9781484209899

More Books

Students also viewed these Databases questions