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

Slides:



Advertisements
Similar presentations
Character Arrays (Single-Dimensional Arrays) A char data type is needed to hold a single character. To store a string we have to use a single-dimensional.
Advertisements

Strings Input/Output scanf and printf sscanf and sprintf gets and puts.
Structures, Unions, and Typedefs CIS 1057 Fall Structures, Unions, and Typedefs CIS 1057 Computer Programming in C Fall 2013 (Many slides based on/borrowed.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 14 - Advanced C Topics Outline 14.1Introduction.
File AccessCS-2301, B-Term File Access CS-2301, System Programming for Non-Majors (Slides include materials from The C Programming Language, 2 nd.
Strings and Dynamic Memory Allocation CS-2301, B-Term Programming Assignment #6 Strings and Dynamic Memory Allocation CS-2301, System Programming.
Scripting Languages Chapter 6 I/O Basics. Input from STDIN We’ve been doing so with $line = chomp($line); Same as chomp($line= ); line input op gives.
Structures, Unions, and Typedefs CS-2301 D-term Structures, Unions, and Typedefs CS-2301 System Programming D-term 2009 (Slides include materials.
Differences between Java and C CS-2303, C-Term Differences between Java and C CS-2303, System Programming Concepts (Slides include materials from.
Display a 12-Month Calendar CS-2301 D-term Programming Assignment #2 12-Month Calendar CS-2301 System Programming C-term 2009 (Slides include materials.
Review of Exam #2CS-2301, B-Term Review of Exam #2 CS-2301, System Programming for Non-Majors (Slides include materials from The C Programming Language,
More Miscellaneous Topics CS-2301 B-term More Miscellaneous Topics CS-2301, System Programming for Non-majors (Slides include materials from The.
More on Numerical Computation CS-2301 B-term More on Numerical Computation CS-2301, System Programming for Non-majors (Slides include materials from.
Homework #5, Binary Trees CS-2301 B-term Homework #5 Binary Trees CS-2301, System Programming for Non-majors (Slides include materials from The C.
CS-341 Dick Steflik Introduction. C++ General purpose programming language A superset of C (except for minor details) provides new flexible ways for defining.
Introduction to FunctionsCS-2301 B-term Introduction to Functions CS-2301, System Programming for Non-majors (Slides include materials from The.
Structures and UnionsCS-2301 B-term Structures and Unions CS-2301, System Programming for Non-majors (Slides include materials from The C Programming.
Miscellaneous topicsCS-2301 B-term Miscellaneous Topics CS-2301, System Programming for Non-majors (Slides include materials from The C Programming.
Guide To UNIX Using Linux Third Edition
C Programming. C vs C++ C syntax and C++ syntax are the same but... C is not object oriented * There is no string class * There are no stream objects.
Assignment #2, 12- month Calendar CS-2301, B-Term Programming Assignment #2 12-Month Calendar CS-2301, System Programming for Non-Majors (Slides.
CS-2303 System Programming Concepts
Console and File I/O - Basics Rudra Dutta CSC Spring 2007, Section 001.
1 Homework Introduction to HW7 –Complexity similar to HW6 –Don’t wait until last minute to start on it File Access will be needed in HW8.
File IO and command line input CSE 2451 Rong Shi.
Digital Computer Concept and Practice Copyright ©2012 by Jaejin Lee C Language Part 4.
File I/O, Project 1: List ADT Bryce Boe 2013/07/02 CS24, Summer 2013 C.
CMSC 202 Java Console I/O. July 25, Introduction Displaying text to the user and allowing the user to enter text are fundamental operations performed.
1 File Handling. 2 Storage seen so far All variables stored in memory Problem: the contents of memory are wiped out when the computer is powered off Example:
1 Lecture09: File I/O 11/19/2012 Slides modified from Yin Lou, Cornell CS2022: Introduction to C.
Lecture 8a: File I/O BJ Furman 21MAR2011. Learning Objectives Explain what is meant by a data stream Explain the concept of a ‘file’ Open and close files.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 21 - C++ Stream Input/Output Basics Outline 21.1Introduction 21.2Streams Iostream Library.
Memory Layout, File I/O Bryce Boe 2013/06/27 CS24, Summer 2013 C.
Chapter 11: Data Files and File Processing Files and streams Creating a sequential access file Reading data from a sequential access file Using fgetc()
Basic I/O in C Computer Organization I 1 August 2009 © McQuain, Feng & Ribbens Stream Model of I/O header file: A stream provides a connection.
Chapter -7 Basic function of Input/output system basics and file processing Stream classes : I/O Streams. A stream is a source or destination for collection.
Minimal standard C program int main(void) { return 0 ; }
Announcements Assignment 1 due Wednesday at 11:59PM Quiz 1 on Thursday 1.
CS 1704 Introduction to Data Structures and Software Engineering.
Files A collection of related data treated as a unit. Two types Text
C is a high level language (HLL)
Files. FILE * u In C, we use a FILE * data type to access files. u FILE * is defined in /usr/include/stdio.h u An example: #include int main() { FILE.
Week 6 - Friday.  What did we talk about last time?  Pointers  Passing values by reference.
DYNAMIC MEMORY ALLOCATION. Disadvantages of ARRAYS MEMORY ALLOCATION OF ARRAY IS STATIC: Less resource utilization. For example: If the maximum elements.
Silberschatz and Galvin  C Programming Language Kingdom of Saudi Arabia Ministry of Higher Education Al-Majma’ah University College of Education.
Chapter 22 – part a Stream refer to any source of input or any destination for output. Many small programs, obtain all their input from one stream usually.
File Access (7.5) CSE 2031 Fall July 2018.
File I/O.
Introduction to C CSE 2031 Fall /3/ :33 AM.
CGS 3460, Lecture 41 Apr 21, 2006 Hen-I Yang
CSC215 Lecture Input and Output.
Plan for the Day: I/O (beyond scanf and printf)
CS111 Computer Programming
File Input/Output.
Programming in C Input / Output.
File I/O We are used to reading from and writing to the terminal:
I/O in C Lecture 6 Winter Quarter Engineering H192 Winter 2005
Chapter 14 - Advanced C Topics
Makefiles and Notes on Programming Assignment PA2
File Input and Output.
Programming Assignment #1 12-Month Calendar—
Accessing Files in C Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2nd edition,
Scope Rules and Storage Types
Differences between Java and C
Homework Applied for cs240? (If not, keep at it!) 8/10 Done with HW1?
Module 12 Input and Output
Introduction to C EECS May 2019.
CS1100 Computational Engineering
Introduction to C CSE 2031 Fall /15/2019 8:26 AM.
File I/O We are used to reading from and writing to the terminal:
Presentation transcript:

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)

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

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

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

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

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

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

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?

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

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?

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'

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

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

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

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);

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

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

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

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

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;

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

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

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