Homework 4 Strings Arrays and malloc CS2301 System

  • Slides: 23
Download presentation
Homework #4 Strings, Arrays, and malloc() CS-2301, System Programming for Non -majors (Slides include

Homework #4 Strings, Arrays, and malloc() CS-2301, System Programming for Non -majors (Slides include materials from The C Programming Language, 2 nd ed. , by Kernighan and Ritchie and from C: How to Program, 5 th ed. , by Deitel and Deitel) CS-2301 B-term 2008 Homework #4 1

Homework #4 • Due Friday, December 5, 11: 59 PM • Assignment: – –

Homework #4 • Due Friday, December 5, 11: 59 PM • Assignment: – – Read text from one or more files – Justify the text so that right and left margins line up – Print justified text • Somewhat bigger than previous homeworks CS-2301 B-term 2008 Homework #4 2

Objectives • Get and interpret arguments from the command line • Develop a program

Objectives • Get and interpret arguments from the command line • Develop a program from multiple C files • Read input from file • Write output to file and/or stderr • Learn to use malloc(), realloc(), and free() • Work with strings and arrays of pointers CS-2301 B-term 2008 Homework #4 3

Background Reading in Kernighan & Ritchie • Chapter 7 – especially • § 7.

Background Reading in Kernighan & Ritchie • Chapter 7 – especially • § 7. 5, File input and output • § 7. 8, Miscellaneous functions • Chapter 4 • § 4. 11. 1, File inclusion • § 4. 11. 3, Conditional inclusion CS-2301 B-term 2008 Homework #4 4

Definition – Module • A C program that does not stand on its own

Definition – Module • A C program that does not stand on its own • Not complete • Unable to execute when compiled • Requires other C programs to be compiled, linked, with it before it can run • (Usually) one of many C programs that together partition a large problem into small pieces • More tractable than one large program • (Often) can be tested independently of larger system CS-2301 B-term 2008 Homework #4 5

Program Structure for Homework #4 • At least three modules – hw 4. c

Program Structure for Homework #4 • At least three modules – hw 4. c • Contains the main() function and any utility functions • Reads and interprets command line – hw 4 Read. And. Print. c • Reads text from a file, invokes justify() function, prints justified lines – hw 4 Justify. c • Implements justify() function • To convert unjustified text to justified lines CS-2301 B-term 2008 Homework #4 6

Program Structure (continued) • Common include file – hw 4. h – Function headers

Program Structure (continued) • Common include file – hw 4. h – Function headers for principle functions – Any common data structures • Included in each module – #include "hw 4. h" • Note straight quotes, instead of < > • Compiled with gcc –Wall –o hw 4. c hw 4 Read. And. Print. c hw 4 Justify. c CS-2301 B-term 2008 Homework #4 7

Program Structure (continued) • Common include file – hw 4. h – Function headers

Program Structure (continued) • Common include file – hw 4. h – Function headers for principle functions – Any common data structures • Included in each module – #include "hw 4. h" • Note straight quotes, instead of • Compiled with d in e d clu ile? n i h p. m 4 o hw s to c t ’ n < > hy is of file W s list thi gcc –Wall –o hw 4. c hw 4 Read. And. Print. c hw 4 Justify. c CS-2301 B-term 2008 Homework #4 8

Homework #4 Program Operation. /hw 4 –w 100 –t 5 file 1. txt file

Homework #4 Program Operation. /hw 4 –w 100 –t 5 file 1. txt file 2. txt. . . • Optional arguments • -w 100, -wn – specifies how wide the justified lines should be (in characters) • -t 5, -tn – specifies the tab spacing • Defaults to 80 characters wide and 5 character tabs • Mandatory arguments • File names • Each file contains text to justify • Sample files provided – see assignment CS-2301 B-term 2008 Homework #4 9

Homework #4 Program Operation. /hw 4 –w 100 –t 5 file 1. txt file

Homework #4 Program Operation. /hw 4 –w 100 –t 5 file 1. txt file 2. txt. . . • The p Optional arguments the rogram nam – specifies nam “. / -wn • -w 100, how wide the justified lines e ” n of i e is eed characters) t should be (in ed? s file. simply Wh • -t 5, -tn – specifies they itab s spacing • Defaults to 80 characters wide and 5 character tabs • Mandatory arguments • File names • Each file contains text to justify • Sample files provided – see assignment CS-2301 B-term 2008 Homework #4 10

Definition – Justify text • Partition text into lines • No more characters than

Definition – Justify text • Partition text into lines • No more characters than line width • Insert additional spaces to align right edges • Except if text ends with '' • If 't' is encountered, replace with spaces • To next multiple of tab spacing 't' • Left edges of lines following 't' aligned under character following 't' CS-2301 B-term 2008 Homework #4 11

Function main() • Process arguments in command line in a loop • For each

Function main() • Process arguments in command line in a loop • For each argument i, if argv[i] is – -t or -w, set tab spacing or width to following number (no space) – Otherwise, treat it as a file name • If a file name – Open file – Call Read. And. Print() – Close file CS-2301 B-term 2008 Homework #4 12

Digression – File I/O FILE *fopen(char *name, char *mode); • Opens file with pathname

Digression – File I/O FILE *fopen(char *name, char *mode); • Opens file with pathname • File mode specifies kind of access • Read only • Read write • Create, etc. • Returns pointer to FILE • NULL if an error • For this application, call • fp = fopen(argv[i], "r"); CS-2301 B-term 2008 Homework #4 //readonly 13

File I/O (continued) • int fclose(FILE *fp); – Closes the file pointed by fp

File I/O (continued) • int fclose(FILE *fp); – Closes the file pointed by fp – Returns zero if successful, EOF if error • getc(FILE *stream) fgetc(FILE *stream) – Returns one character from stream or EOF at end of file or if error • fprintf(FILE *stream, char* format, . . . ) – Same as printf, but to file denoted by stream • Built-in file pointers of type FILE * – stdin, stdout, stderr CS-2301 B-term 2008 Homework #4 14

Function main() • Process arguments in command line in a loop • For each

Function main() • Process arguments in command line in a loop • For each argument i, if argv[i] is – -t or -w, set tab spacing or width to following number (no space) – Otherwise, treat it as a file name • If a file name – Open file using fp = fopen(argv[i], "r"); – Call Read. And. Print(fp, stdout, width, tab) – Close file using fclose(fp); CS-2301 B-term 2008 Homework #4 15

Read. And. Print() • Parameters: – – FILE *input, FILE *output – const int

Read. And. Print() • Parameters: – – FILE *input, FILE *output – const int width, const int tab • Allocates char *buffer using malloc() – Suitable size (defined in hw 4. h) • Reads text from input – One character at a time using fgetc() – Keep track of size of array, # of chars already in array – If necessary, use realloc() to increase size of array • When 'n' of EOF is encountered, – Terminate string with '' – Call justify() • … CS-2301 B-term 2008 Homework #4 16

Read. And. Print() (continued) • … justify() returns array of pointers to char *

Read. And. Print() (continued) • … justify() returns array of pointers to char * • i. e. , char ** • Each pointer points to a line of justified text ending in '' • Read. And. Print() prints text to output • fprintf(output, "%sn", line[i]); where line[i] is ith line returned • After printing ith line, free(line[i]); • After printing all lines, free array of pointers CS-2301 B-term 2008 Homework #4 17

Code Fragment for Read. And. Print char *buffer = malloc(default. Buf. Size); int buf.

Code Fragment for Read. And. Print char *buffer = malloc(default. Buf. Size); int buf. Size = default. Buf. Size; int position = 0; while ((chr=fgetc(input))!= 'n') { if (rc==EOF) break; if (position>=buf. Size-1) { buf. Size += default. Buf. Size; buffer = realloc(buffer, buf. Size); } buffer[position++]=chr; } CS-2301 B-term 2008 Homework #4 18 buffer[position] = '';

justify function char **justify(const char *text, const int width, const int tab); • Allocate

justify function char **justify(const char *text, const int width, const int tab); • Allocate array of pointers • For each line • Allocate a character array of size width+1 store pointer in array of pointers • Fill character array with some text up to white space • Insert blanks to justify text • If end, add null pointer • If array of pointers is full, increase size CS-2301 B-term 2008 Homework #4 19

Code Fragment for Justify char *line[] = malloc(default. Arr. Size); int arr. Size =

Code Fragment for Justify char *line[] = malloc(default. Arr. Size); int arr. Size = default. Arr. Size; int line. Num = 0, next. Chr = 0; while (text remaining) { if (line. Num>=arr. Size-1) { arr. Size += default. Arr. Size; line = realloc(line, arr. Size); } line[line. Num]=malloc(width+1); next. Chr = justify. One(line[line. Num], text[next. Chr], width, tab); line. Num++; } line[line. Num] = NULL; CS-2301 B-term 2008 Homework #4 20

justify. One() • Exercise for the student! CS-2301 B-term 2008 Homework #4 21

justify. One() • Exercise for the student! CS-2301 B-term 2008 Homework #4 21

Summary — Homework #4 • Multi-module assignment • Read and justify text from input

Summary — Homework #4 • Multi-module assignment • Read and justify text from input file(s) • Specified on command line • Call malloc() to allocate storage for – Text strings – Array of pointers to text strings • Call realloc() to change size of array • Print errors on stderr CS-2301 B-term 2008 Homework #4 22

Questions? Homework #4 due Friday, December 5, 11: 59 PM CS-2301 B-term 2008 Homework

Questions? Homework #4 due Friday, December 5, 11: 59 PM CS-2301 B-term 2008 Homework #4 23