1CMSC 212 – S07 (lect 6)AnnouncementsProgram #2– Due two weeks from todayReading– Chapter 15 (today)• skip 15.10.1 and 15.10.2 (scanf)– Chapter 112CMSC 212 – S07 (lect 6)Project NotesStart Early! - It's longer than P1Helpful functions– int strncpy(char *s1, char *s2, int max);– size_t strspn(const char *, const char *);– size_t strcspn(const char *, const char *);– int isalnum(char);– void perror(char *message);– void exit(int status);Suggestions– Start simple• write command line argument code• write code to read input file• write code to parse and/or modify input line as needed• Test and add code in steps3CMSC 212 – S07 (lect 6)Printf (the rest of the story)int fprintf(FILE *stream, char *format, …);– sends output to streamprintf(char *format, …);– sends output to stdoutsnprintf(char *buffer, int limit, char *format, …);– sends output the the character buffer– prints at most limit-1 characters to buffer• adds null to end of stringnever! use sprintf (due to buffer overflow)4CMSC 212 – S07 (lect 6)Printf format codesc print argument as unsigned characterd print as decimal integeru print as unsigned integerx print as hex (use X for capital A-F in format)e print in exponent form (6.02300e23)f print in floating point formats print as a string (null terminated character array)% print a % (so %% prints one %)5CMSC 212 – S07 (lect 6)Printf – Flags and Field WidthsFlags– - left justify the field (default is right)– + for signed numbers force the + to be printed– #• For x and X ensures a leading 0x or 0XPrecision – How many characters will be printed– Format is x.y• For int types – x is total width and y is numeric digits– Leading 0’s are added to bring width up to y• For floating points numbers – X is total width and y is digits right of decimal point6CMSC 212 – S07 (lect 6)Format code examplesABCDEFGHABC..A….%-5sABCDE..ABC….A%5.5sABCDEABCA%.5sABCDEFGH..ABC….A%5sABCDEFGHABCA%sABCDEFGHABCAFormat Code+123456789+12345-12+1%+d12345678912345-0120001%04d12345678912345-12.1…%-4d123456789.12345.-0012..0001%6.4d12345678912345-00120001%.4d123456789.12345…-12…..1%6d12345678912345-121%d12345678912345-121Format7CMSC 212 – S07 (lect 6)Binary I/OWrite out data structures in raw format– Tends to be non-portablesize_t fread(void *buffer, size_t size, size_t count, FILE *stream);– Read into buffer, count items of size size from stream– Returns the number of items read– ret = fread(mystuff, sizeof(struct foo), 42, stdin);size_t fwrite(void *buffer , size_t size, size_t count, FILE *stream);– Write to stream, count items of size size starting at buffer– Return the number of items written8CMSC 212 – S07 (lect 6)Other FILE Operationsint fflush(FILE *fp);– Write any buffered output to the destinationlong ftell(FILE *stream);– Return the current position (offset) of the stream– Return value is in bytesint fseek(FILE *stream, long offset, int from);– Change the position of the stream– Where depends on value of from• SEEK_SET - offset bytes from start of file• SEEK_CUR - offset bytes from the current position• SEEK_END - offset bytes from the end of the file• Last two may be positive or negative9CMSC 212 – S07 (lect 6)Misc. FILE FunctionsError and Status– int feof(FILE *stream);• Returns true if currently at end of file– int ferror(FILE *stream);• Returns true if any error condition is true– void clearerr(FILE *stream);• Reset the error indication for the fileTemporary Files– Sometimes need a file that is temporary• But it needs to be unique– FILE *tmpfile(void);• Create a new file that, it will be deleted on close10CMSC 212 – S07 (lect 6)Make: A Tool to Compile ProgramsProblem: Compiling programs is tediousSolution: Automate it!Done in Unix by the make command– Uses a file called Makefile• A makefile is a file (script) containing :– Project structure (files, dependencies)– Instructions for files creationMake is not limited to C programsAdapted from: www.cs.tau.ac.il/~dcor/Software1/Makefile.ppt11CMSC 212 – S07 (lect 6)Describing Files to MakeMakefile contains project dependencies– represented as a DAG (= Directed Acyclic Graph) Example:– Program contains 3 files– main.c., sum.c, sum.h– sum.h included in both .c files– Executable should be the file sumsum (exe)sum.omain.osum.csum.hmain.c12CMSC 212 – S07 (lect 6)Makefile for Previous Examplesum: main.o sum.ogcc –o sum main.o sum.omain.o: main.c sum.hgcc –c main.c sum.o: sum.c sum.hgcc –c sum.c Notes About Makefiles:– Target is first item in a dependency– Items after “:” required to build target– Actions must have a tab at start– First target in file is the default rootTarget DependencyTabAction13CMSC 212 – S07 (lect 6)Make’s OperationProject dependencies tree is constructedA target is out of date when one of its dependencies is newer than itself.If out of date, recreate the target– Perform action specified– Done on the way up the tree. Notes:– Ensures minimum compilation, with proper project structure– Avoid writing something like:prog: main.c sum1.c sum2.cgcc –o prog main.c sum1.c sum2.c• requires full compilation when something changes14CMSC 212 – S07 (lect 6)Additional Make FeaturesMacros– <variable> = <value>– Example:CC = gccCFLAGS = -g -WallFoo.o: foo.c$(CC) $(CFLAGS) –c foo.cDefault Action– $(CC) –c $(CFLAGS)Actions can sometimes delete files not create them– Example:Clean:rm *.o15CMSC 212 – S07 (lect 6)More Complex Makefileall: intCount tableclean:rm -f *.o *.bb *.bbg *.da *.pngintCount: intCount.o$(CC) -o intCount intCount.otable: table.o main.o hashTable.o$(CC) -o table table.o main.o hashTable.otable.o: table.c table.hhashTable.o: hashTable.c table.hmain.o: main.c table.hCC = gccCFLAGS = -g -WallTwo top-level programsLink together several filesUses Default RuleMacro DefinitionsNot part of main treebuilt by typing “make
View Full Document