Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Homework #4CS-2301 B-term 20081 Homework #4 Strings, Arrays, and malloc() CS-2301, System Programming for Non-majors (Slides include materials from The."— Presentation transcript:

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

2 Homework #4CS-2301 B-term 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

3 Homework #4CS-2301 B-term 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

4 Homework #4CS-2301 B-term 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

5 Homework #4CS-2301 B-term 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

6 Homework #4CS-2301 B-term 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

7 Homework #4CS-2301 B-term 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 with gcc –Wall –o hw4 hw4.c hw4ReadAndPrint.c hw4Justify.c

8 Homework #4CS-2301 B-term 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 with gcc –Wall –o hw4 hw4.c hw4ReadAndPrint.c hw4Justify.c Why isn’t hw4.h included in this list of files to compile?

9 Homework #4CS-2301 B-term 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

10 Homework #4CS-2301 B-term 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?

11 Homework #4CS-2301 B-term 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'

12 Homework #4CS-2301 B-term 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

13 Homework #4CS-2301 B-term 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

14 Homework #4CS-2301 B-term 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

15 Homework #4CS-2301 B-term 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);

16 Homework #4CS-2301 B-term 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() …

17 Homework #4CS-2301 B-term 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

18 Homework #4CS-2301 B-term 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';

19 Homework #4CS-2301 B-term 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

20 Homework #4CS-2301 B-term 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;

21 Homework #4CS-2301 B-term justifyOne() Exercise for the student!

22 Homework #4CS-2301 B-term 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

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


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

Similar presentations


Ads by Google