homework #4cs-2301 b-term 20081 homework #4 strings, arrays, and malloc() cs-2301, system...

23
Homework #4 CS-2301 B-term 200 8 1 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)

Upload: blaze-hawkins

Post on 16-Dec-2015

219 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Homework #4CS-2301 B-term 20081 Homework #4 Strings, Arrays, and malloc() CS-2301, System Programming for Non-majors (Slides include materials from The

Homework #4CS-2301 B-term 2008 1

Homework #4Strings, Arrays, and malloc()

CS-2301, System Programming for Non-majors

(Slides include materials from The C Programming Language, 2nd ed., by Kernighan and Ritchie and from C: How to Program, 5th ed., by Deitel and Deitel)

Page 2: Homework #4CS-2301 B-term 20081 Homework #4 Strings, Arrays, and malloc() CS-2301, System Programming for Non-majors (Slides include materials from The

Homework #4CS-2301 B-term 2008 2

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

Page 3: Homework #4CS-2301 B-term 20081 Homework #4 Strings, Arrays, and malloc() CS-2301, System Programming for Non-majors (Slides include materials from The

Homework #4CS-2301 B-term 2008 3

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

Page 4: Homework #4CS-2301 B-term 20081 Homework #4 Strings, Arrays, and malloc() CS-2301, System Programming for Non-majors (Slides include materials from The

Homework #4CS-2301 B-term 2008 4

Background Reading inKernighan & 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

Page 5: Homework #4CS-2301 B-term 20081 Homework #4 Strings, Arrays, and malloc() CS-2301, System Programming for Non-majors (Slides include materials from The

Homework #4CS-2301 B-term 2008 5

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

Page 6: Homework #4CS-2301 B-term 20081 Homework #4 Strings, Arrays, and malloc() CS-2301, System Programming for Non-majors (Slides include materials from The

Homework #4CS-2301 B-term 2008 6

Program Structure for Homework #4

• At least three modules– hw4.c

• Contains the main() function and any utility functions

• Reads and interprets command line– hw4ReadAndPrint.c

• Reads text from a file, invokes justify() function, prints justified lines

– hw4Justify.c• Implements justify() function• To convert unjustified text to justified lines

Page 7: Homework #4CS-2301 B-term 20081 Homework #4 Strings, Arrays, and malloc() CS-2301, System Programming for Non-majors (Slides include materials from The

Homework #4CS-2301 B-term 2008 7

Program Structure (continued)

• Common include file – hw4.h– Function headers for principle functions– Any common data structures

• Included in each module– #include "hw4.h"

• Note straight quotes, instead of < >

• Compiled withgcc –Wall –o hw4 hw4.c hw4ReadAndPrint.c hw4Justify.c

Page 8: Homework #4CS-2301 B-term 20081 Homework #4 Strings, Arrays, and malloc() CS-2301, System Programming for Non-majors (Slides include materials from The

Homework #4CS-2301 B-term 2008 8

Program Structure (continued)

• Common include file – hw4.h– Function headers for principle functions– Any common data structures

• Included in each module– #include "hw4.h"

• Note straight quotes, instead of < >

• Compiled withgcc –Wall –o hw4 hw4.c hw4ReadAndPrint.c hw4Justify.c

Why isn’t hw4.h

included in

this list of file

s to compile?

Page 9: Homework #4CS-2301 B-term 20081 Homework #4 Strings, Arrays, and malloc() CS-2301, System Programming for Non-majors (Slides include materials from The

Homework #4CS-2301 B-term 2008 9

Homework #4 Program Operation

./hw4 –w100 –t5 file1.txt file2.txt ...

• Optional arguments•-w100, -wn – specifies how wide the justified lines

should be (in characters)•-t5, -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

Page 10: Homework #4CS-2301 B-term 20081 Homework #4 Strings, Arrays, and malloc() CS-2301, System Programming for Non-majors (Slides include materials from The

Homework #4CS-2301 B-term 2008 10

Homework #4 Program Operation

./hw4 –w100 –t5 file1.txt file2.txt ...

• Optional arguments•-w100, -wn – specifies how wide the justified lines

should be (in characters)•-t5, -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

The program name is simply

the name of its file. Why is

“./” needed?

Page 11: Homework #4CS-2301 B-term 20081 Homework #4 Strings, Arrays, and malloc() CS-2301, System Programming for Non-majors (Slides include materials from The

Homework #4CS-2301 B-term 2008 11

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 '\0'

• 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'

Page 12: Homework #4CS-2301 B-term 20081 Homework #4 Strings, Arrays, and malloc() CS-2301, System Programming for Non-majors (Slides include materials from The

Homework #4CS-2301 B-term 2008 12

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 ReadAndPrint()

– Close file

Page 13: Homework #4CS-2301 B-term 20081 Homework #4 Strings, Arrays, and malloc() CS-2301, System Programming for Non-majors (Slides include materials from The

Homework #4CS-2301 B-term 2008 13

Digression – File I/O

FILE *fopen(char *name, char *mode);

• Opens file with pathname name• 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"); //readonly

Page 14: Homework #4CS-2301 B-term 20081 Homework #4 Strings, Arrays, and malloc() CS-2301, System Programming for Non-majors (Slides include materials from The

Homework #4CS-2301 B-term 2008 14

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

Page 15: Homework #4CS-2301 B-term 20081 Homework #4 Strings, Arrays, and malloc() CS-2301, System Programming for Non-majors (Slides include materials from The

Homework #4CS-2301 B-term 2008 15

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 ReadAndPrint(fp, stdout, width, tab)– Close file using fclose(fp);

Page 16: Homework #4CS-2301 B-term 20081 Homework #4 Strings, Arrays, and malloc() CS-2301, System Programming for Non-majors (Slides include materials from The

Homework #4CS-2301 B-term 2008 16

ReadAndPrint()

• Parameters:–– FILE *input, FILE *output– const int width, const int tab

• Allocates char *buffer using malloc()– Suitable size (defined in hw4.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 '\0' – Call justify()

• …

Page 17: Homework #4CS-2301 B-term 20081 Homework #4 Strings, Arrays, and malloc() CS-2301, System Programming for Non-majors (Slides include materials from The

Homework #4CS-2301 B-term 2008 17

ReadAndPrint() (continued)

• …justify() returns array of pointers to char *

• i.e., char **• Each pointer points to a line of justified text ending in '\0'

• ReadAndPrint() prints text to output• fprintf(output, "%s\n", line[i]); where line[i] is ith line returned

• After printing ith line, free(line[i]);• After printing all lines, free array of pointers

Page 18: Homework #4CS-2301 B-term 20081 Homework #4 Strings, Arrays, and malloc() CS-2301, System Programming for Non-majors (Slides include materials from The

Homework #4CS-2301 B-term 2008 18

Code Fragment for ReadAndPrint

char *buffer = malloc(defaultBufSize);int bufSize = defaultBufSize;int position = 0;

while ((chr=fgetc(input))!= '\n') {if (rc==EOF) break;if (position>=bufSize-1) {

bufSize += defaultBufSize;buffer = realloc(buffer,bufSize);

}buffer[position++]=chr;

}

buffer[position] = '\0';

Page 19: Homework #4CS-2301 B-term 20081 Homework #4 Strings, Arrays, and malloc() CS-2301, System Programming for Non-majors (Slides include materials from The

Homework #4CS-2301 B-term 2008 19

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+1store 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

Page 20: Homework #4CS-2301 B-term 20081 Homework #4 Strings, Arrays, and malloc() CS-2301, System Programming for Non-majors (Slides include materials from The

Homework #4CS-2301 B-term 2008 20

Code Fragment for Justify

char *line[] = malloc(defaultArrSize);int arrSize = defaultArrSize;int lineNum = 0, nextChr = 0;

while (text remaining) {if (lineNum>=arrSize-1) {

arrSize += defaultArrSize;line = realloc(line,arrSize);

}line[lineNum]=malloc(width+1);nextChr = justifyOne(line[lineNum],

text[nextChr], width, tab);lineNum++;

}

line[lineNum] = NULL;

Page 21: Homework #4CS-2301 B-term 20081 Homework #4 Strings, Arrays, and malloc() CS-2301, System Programming for Non-majors (Slides include materials from The

Homework #4CS-2301 B-term 2008 21

justifyOne()

• Exercise for the student!

Page 22: Homework #4CS-2301 B-term 20081 Homework #4 Strings, Arrays, and malloc() CS-2301, System Programming for Non-majors (Slides include materials from The

Homework #4CS-2301 B-term 2008 22

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

Page 23: Homework #4CS-2301 B-term 20081 Homework #4 Strings, Arrays, and malloc() CS-2301, System Programming for Non-majors (Slides include materials from The

Homework #4CS-2301 B-term 2008 23

Questions?

Homework #4 dueFriday, December 5, 11:59 PM